views:

261

answers:

2

I know I have that my connection to the database works, and a test I did using no auto-increment id worked fine for me. The code below refuses to work and I can't find a a way around it. My table has 3 columns, ID (auto increment), name and value. What do I need to change in order to get this to work?

Thanks in advance

//create placeholder query $query = "INSERT INTO prepared_test (name, value) VALUES (?,?)";

//prepare the query $stmt = mysqli_prepare($connection, $query);

$name = 'steve'; $value = 45;

mysqli_bind_param($stmt, array(MYSQLI_BIND_STRING, MYSQLI_BIND_INT), $name, $value); mysqli_execute($stmt);

if(mysqli_stmt_affected_rows($stmt) != 1) die("issues");

mysqli_stmt_close($stmt);

$connection->close();

A: 

I'd recommend you using PDO (http://ar2.php.net/PDO). It's an object oriented interface for different databases, it has a cleaner syntax and abstracts you from the RDBMS ;)

alcuadrado
He asked why he cant insert and not which has scleaner syntax.
streetparade
Yeah, I just gave a piece of advice :)
alcuadrado
+3  A: 

mysqli_bind_param is deprecated and is just an alias of mysqli_stmt_bind_param, mysqli_stmt_bind_param expects the bind types to be a string not an array. so just change that line.

mysqli_stmt_bind_param($stmt, 'si', $name, $value);
RMcLeod
Cheers man, worked a treat! :)
Drew