dbear05 Posted April 6, 2013 Share Posted April 6, 2013 Hello! Hows everyone? I need help adding data to my sql. I am able to connect to my sql. When I run the code in the while loop "No cameras found" appears. <?php# Desc: Add camera information to the databaserequire ('/home/share/formhelpernew.php');require ('dbstuff.php');if ($_POST) if ($form_errors = validate_form()) show_errors($form_errors); else process_form( );else print "Please <a href='camera_add.htm'>go here</a> and add a camera.";// end of main program logicfunction validate_form() { $errors = array(); // make sure all required data was entered if ($_POST['mpx'] != strval(floatval($_POST['mpx']))) $errors[] = 'Please enter a numeric value for mpx.'; if ($_POST['price'] != strval(floatval($_POST['price']))) $errors[] = 'Please enter a numeric value for price.'; if (strlen($_POST['company']) == 0) $errors[] = 'Please enter a company name.'; if (strlen($_POST['model']) == 0) $errors[] = 'Please enter a product model.'; return $errors;} // end validate!// Just displays error and says "hit back arrow to fix"function show_errors($err) { print "<h1>Errors! </h1>\n"; print "Click the back arrow and fix the following errors:\n<ol>\n"; foreach ($err as $error) print "<li>$error</li>\n"; print "</ol>\n";}function process_form( ) { $db=connectdb( ); $sth = $db->prepare( "insert into cameras ( company,model,mpx,price) values('?')"); $sth->execute(array(company, model, mpx, price, id));if ($sth->rowCount()) while ($row = $sth->fetch()) echo " Insert Successful<br />"; else echo "No cameras found<br />"; // This query assumes autoincrement set on item_id field... $query = "insert into cameras (company, model, mpx, price, id) " ; // Values to put into placeholders $qvals_array = array($_POST['company'], "enter more data here ... " ); //Uuncomment these when you get it correct ... $sth=$db->prepare($query); $sth->execute($qvals_array); $item_id = $db->lastInsertID(); // check to make sure that countRows() equals 1 ... if so, print success, // otherwise, error! Either way, provide link back to dashboard.}?> Quote Link to comment https://forums.phpfreaks.com/topic/276620-insert-data-from-php-to-mysql/ Share on other sites More sharing options...
Jessica Posted April 6, 2013 Share Posted April 6, 2013 A. Please use code tags on our forum. B. You should really start using curly braces on your conditionals, it makes them much more readable. C. $sth = $db->prepare( "insert into cameras ( company,model,mpx,price) values('?')"); $sth->execute(array(company, model, mpx, price, id)); Way off.1. Parameters either need to be positional (?) or named (:name). You need one for EACH parameter, and you don't put them in quotes. 2. The second line will produce warnings/notices. You either need to use the $_POST array or pass in the values as variables. Right now you're using constants which will all be set to strings. You also need as many values as you have named columns/parameters. D. Why are you trying to do the insert twice? (Both very wrong.) E. When you have an auto-increment ID, don't try to insert a value into the field. Just leave the column name out of the query. I'm sure there's more but that's enough for me. Quote Link to comment https://forums.phpfreaks.com/topic/276620-insert-data-from-php-to-mysql/#findComment-1423311 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.