0

I have a simple query that I'm using to insert a record based on a form submission, but it's not writing the record to the database. It's also not throwing an error. I used a var_dump and verified that the variables are posting correctly, so I think the issue is in the query syntax, but I've checked that too.

Any help or guidance is greatly appreciated. Here is the code:

if (isset($submit)){

$user_id = $_SESSION['user_id'];
$anthem1 = $_POST['anthem1'];
$cointoss2 = $_POST['cointoss2'];

$query = "INSERT INTO mypicks (";
$query .= "  user_id, anthem1, cointoss2";
$query .= ") VALUES (";
$query .= "  '{$user_id}', '{$anthem1}', '{$cointoss2}'";
$query .= ")";
$result = mysqli_query($connection, $query);

}
5
  • The query syntax is correct. Have you checked $submit value? (var_dump( $submit ); )
    – fusion3k
    Commented Mar 24, 2016 at 1:25
  • Of course there is no error, since you do not do any sql error handling by checking the return value from mysqli_query(). I hope you do connect to a mysql server in your code.
    – Shadow
    Commented Mar 24, 2016 at 1:26
  • Add an or die(mysql_error($connection)) to the end of mysqli_query($connection, $query) to get your error message, ie. $result = mysqli_query($connection, $query) or die(mysql_error($connection));
    – Sean
    Commented Mar 24, 2016 at 1:27
  • 1
    Don't use variable interpolation, use a prepared statement. Your code is subject to SQL-injection because of this.
    – Barmar
    Commented Mar 24, 2016 at 2:08
  • echo $query and run it in the sql console and look for errors there trusting the connection and db select is ok.
    – unixmiah
    Commented Mar 24, 2016 at 3:48

1 Answer 1

1

You'll want to add some checks into your code.

First you'll want to make sure that your database connection is happening:

$connection = new mysqli('localhost', 'username', 'password', 'database');

if($connection->connect_errno > 0){
    die('Unable to connect to database [' . $connection->connect_error . ']');
}

Next thing, you'll want to make sure that your $submit variable is set, so your code block is actually firing.

If it is, make sure that your query is working:

$user_id = $_SESSION['user_id'];
$anthem1 = $_POST['anthem1'];
$cointoss2 = $_POST['cointoss2'];

$stmt = $connection->prepare("INSERT INTO mypicks ( user_id, anthem1, cointoss2 ) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $user_id, $anthem1, $cointoss2);
$stmt->execute();

if(!$result = $stmt->get_result()){
    die('There was an error running the query [' . $connection->error . ']');
}

Also don't forget to clean up after wards

$stmt->close();
$connection->close();
2
  • 1
    Down voted because you could have used prepared statements in your answer. Commented Mar 24, 2016 at 2:59
  • Thanks. The $submit variable wasn't being set properly. Thanks for the help everyone. Commented Mar 24, 2016 at 23:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.