Jump to content

simple prepared statements question


ihopethisworks

Recommended Posts

Hello, thanks for reading --

 

The simple page below should grab a user id ($_SESSION['id']) and message ($_POST['message']) and insert them them to a table using prepared statements. However, currently the script prints '-1 rows affected,' indicating failure, without inserting data. I'm sure I'm overlooking something simple here?

Thanks very much

 

 

<?php session_start();require_once '../includes/constants.php';$con = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);if(mysqli_connect_errno()){    printf("Connect failed: %s\n", mysqli_connect_error());    exit(); }$stmt = $con->prepare("INSERT INTO stories (id, message) VALUES(?,?)");$id = $_SESSION['id'];$message = $_POST['message'];$message = addslashes($message);$stmt->bind_param('is', $id, $message);echo $id . $message;$stmt->execute();printf("%d Row inserted. \n", $stmt->affected_rows);$stmt->close();

 

Link to comment
Share on other sites

Your code works for me, therefore there must be something going on with it on your server. And since you actually have some error checking and reporting logic for the connection, you most likely have a problem with your table or your columns or perhaps the mysqli extension is not enabled.

 

Are you developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php errors would be reported and displayed?

 

$stmt->execute(); will return a FALSE value if the query fails due to an error and you can use $stmt->error to find out why it failed.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.