PHP POST not sending a value to the Database

3391 views php
5

I'm just starting out with PHP and trying to send a value to the DB from a form select option. The new record is being created but there is no value.

$sql = "INSERT INTO here (location) VALUES ('".$_POST["location"]."')";
if (mysqli_query($conn, $sql)) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

And the HTML -

<form method="post" action="vars.php">

<select name='location'>
<option type='text' value='raineault garage'>london bridge</option>
 <option type='text' value='long buckby'>Sun Street</option>
<option type='text' value='london bridge'>Liverpool street</option>
 <option type='text' value='my house'>borough market</option>
 <option type='text' value='newton'>greenwich</option>
<option type='text' value='hackney'>Hackney</option>
<option type='text' value='sadasd'>Oxford Circus</option>
</select>

Would really appreciate if someone can tell me where I'm going wrong

answered question

Question: Are you checking for SQL injection?

Please use PDO at least OP

@GeorgeAppleton mysqli is fine and enables some capabilities that PDO doesn't support, but I do suggest using prepared statements whenever accepting user data.

Check the data type of the field you're trying to insert data in to. Perhaps there is a data type mismatch.

First check your $con. It should be like this mysqli_connect("host","user","password","table"); Also be sure you are closing your connection. mysqli_close($con);

Share the table structure so we can help debug ;)

1 Answer

10

As of this moment, I can't see anything that would be a problem.

So now, we must debug, (or try to find problems in the code).

  1. echo $_POST["location"]. Does it display something raineault garage?
  2. echo $mysql. Does it display something like INSERT INTO here (location) VALUES ('raineault garage')?

I shall await your answer :)

posted this

Have an answer?

JD

Please login first before posting an answer.