Unable to fetch data from database using MySQLi [c

2019-03-07 09:33发布

问题:

I have 2 files, in index.html the user will input a URL and when he clicks on submit then the URL would be logged into the database with an ID and also the user would get that id. Now the problem is that I am able to put the URL in DB but I am not able to fetch the ID to which it has been logged. I am getting all the ID that are stored in the DB! but I just want the ID of the URL that the user has put in index.thml ...Please help me out!

Following is index.html's code

<form action="process.php" method="post">
URL(with "http://"): <input type="text" name="url">
<input type="submit">
</form>  

and this is process.php's code

    <?php
$con=mysqli_connect("localhost","root","","url");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$url = $_POST[url];
$sql="INSERT INTO url (url) VALUES('$url')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "something";
echo '<br><br>';

$result = mysqli_query($con,"SELECT id FROM url WHERE url='$url'");
while($ids = mysqli_fetch_array($result))
  {
  echo "something";
  echo 'http://localhost/redirect/' . $ids['id'];
  }


mysqli_close($con);
?> 

回答1:

$url = $_POST[url];

You missed single quotes around array key.

Right form

$url = $_POST['url'];


回答2:

use {$url} in place of just $url