godster Posted July 1, 2009 Share Posted July 1, 2009 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.... ?> Link to comment https://forums.phpfreaks.com/topic/164437-solved-problems-with-data-param-functionplease-help/ Share on other sites More sharing options...
JJ2K Posted July 1, 2009 Share Posted July 1, 2009 What is line 68?? Link to comment https://forums.phpfreaks.com/topic/164437-solved-problems-with-data-param-functionplease-help/#findComment-867394 Share on other sites More sharing options...
AwptiK Posted July 1, 2009 Share Posted July 1, 2009 this: $q = 'INSERT INTO products (product_name, price, description, image_name) VALUES (?, ?, ?, ?)'; should be this: $q = 'INSERT INTO products (product_name, price, description, image_name) VALUES ("?", "?", "?", "?")'; Link to comment https://forums.phpfreaks.com/topic/164437-solved-problems-with-data-param-functionplease-help/#findComment-867411 Share on other sites More sharing options...
godster Posted July 1, 2009 Author Share Posted July 1, 2009 Line 68 is: mysqli_stmt_bind_param($stmt, 'isdsss', $pn, $p, $d, $i); AwptiK, have tried adding the quotes as suggested, but still recieving the same error. Any other suggestions welcome? Link to comment https://forums.phpfreaks.com/topic/164437-solved-problems-with-data-param-functionplease-help/#findComment-867442 Share on other sites More sharing options...
godster Posted July 2, 2009 Author Share Posted July 2, 2009 Anyone got any suggestions on this please? Its really frustrating me, especially since i'm following a working script... Link to comment https://forums.phpfreaks.com/topic/164437-solved-problems-with-data-param-functionplease-help/#findComment-867633 Share on other sites More sharing options...
Dathremar Posted July 2, 2009 Share Posted July 2, 2009 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 Link to comment https://forums.phpfreaks.com/topic/164437-solved-problems-with-data-param-functionplease-help/#findComment-867636 Share on other sites More sharing options...
godster Posted July 2, 2009 Author Share Posted July 2, 2009 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? Link to comment https://forums.phpfreaks.com/topic/164437-solved-problems-with-data-param-functionplease-help/#findComment-867775 Share on other sites More sharing options...
godster Posted July 2, 2009 Author Share Posted July 2, 2009 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? Link to comment https://forums.phpfreaks.com/topic/164437-solved-problems-with-data-param-functionplease-help/#findComment-867979 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.