Jump to content

[SOLVED] Problems with data param function...please help


godster

Recommended Posts

Hi guys.

 

Trying to follow a script that would provide an form for adding new products to product table in my database.

 

I followed the script through exactly and bug fixed all but a binding issue with the bind-param function. Keep getting the following error when attempting to add products:

 

"Number of elements in type definition string doesn't match number of bind variables in...line 68"

 

Script is as below....please help and suggest where i'm going wrong.

 

<?php # Script 17.1 - add_print.php

//This page allows the administrator to add a product.

require_once('mysqli_connect.php');

if (isset($_POST['submitted'])) { //Handle the form

//Validate the incoming data...

$errors = array();

//Check for a product name:
if (!empty($_POST['product_name'])){
$pn = trim($_POST['product_name']);
} else {
$errors[] = 'Please enter the product\'s name!';
}

//Check for an image:
if (is_uploaded_file ($_FILES['image']['tmp_name'])) {

//Create a temporary file name:
    $temp = 'uploads/' . md5($_FILES['image']['name']);
    
    //Move the file over:
    if (move_uploaded_file($_FILES['image']['tmp_name'], $temp)) {
    
    echo '<p>The file has been uploaded!</p>';
    
    //Set the $i variable to the image's name:
    
    $i = $_FILES['image']['name'];
    
   } else {// Could'nt move the file over.
   $errors[] = 'The file could not be moved.';
   $temp = $_FILES['image']['tmp_name'];
   }
  
} else { //No uploaded file.
$errors[] = 'No file was uploaded';
$temp = NULL;
}

//Check for a price:
if (is_numeric($_POST['price'])) {
$p = (float) $_POST['price'];
    } else {
    	$errors[] = 'Please enster the product\'s price!';
    }
    
//Check for description:

$d = (!empty($_POST['description'])) ? trim($_POST['description']) : NULL;

//Add the product to the database;
$q = 'INSERT INTO products (product_name, price, description, image_name) VALUES (?, ?, ?, ?)';

$stmt = mysqli_prepare($dbc, $q);

mysqli_stmt_bind_param($stmt, 'isdsss', $pn, $p, $d, $i);
mysqli_stmt_execute($stmt);

//Check the results...
if (mysqli_stmt_affected_rows($stmt) == 1) {

//Print a message:

echo '<p>The product has been added.</p>';

//Rename the image:
$id = mysqli_stmt_insert_id($stmt); //Get the product ID
rename ($temp, "/uploads/$id");

//Clear $_POST:
$_POST = array();

} else { //Error!
echo '<p>Your submission could not be processed due to a system error.</p>';
    }
    
    mysqli_stmt_close($stmt);
    
} //End of $errors IF

//Delete the uploaded file if it still exists:
if (isset($temp) && file_exists ($temp) && is_file($temp) ) {
unlink ($temp);
}


//Check for errors and print them:
if (!empty($errors) && is_array($errors) ) {
echo '<h1>Error!</h1>
    <p>The following error(s) occurred:<br/>';
    foreach ($error as $msg) {
    echo " - $msg<br />\n";
    }
    echo 'Please reselect the product image and try again.</p>';
}

//Display the form....
?>

The problem is this:

 

mysqli_stmt_bind_param($stmt, 'isdsss', $pn, $p, $d, $i);

 

The second parameter determines the type of the bind parameters, see this., also this should match the number of bind parameters.

 

You should change it to:

 

mysqli_stmt_bind_param($stmt, 'isds', $pn, $p, $d, $i);

 

I haven't check whats the actual type of your variables so please revise that ;)

Thanks Dathremar...this is starting to make more sense.

 

I guess I need to check the format of the columns in my database and ensure that these match the bind parameters. I also only have 4 values to bind so only need 4 corresponding parameters.

 

$pn = product_name so it think this is 's'

$p = price so possibly 's' again....can someone explain what is meant by 'double' and 'blob'?

$d = description so definitely 's' I would say

$i - image_name....'s' again?

 

The result would be:

 

mysqli_stmt_bind_param($stmt, 'ssss', $pn, $p, $d, $i);

 

....is that right?

Think i've resolved this with you help thanks guys. Just wanted to check though, when using the form to upload an image, its placing a file in my 'uploads' directory as I would expect.

 

The corresponding field in the database has the correct file name, yet in the file folder containing the file it is simply named "1", with no file extension. Is this correct?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.