Jump to content

Insert Data from php to mysql


dbear05

Recommended Posts

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 database

require ('/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 logic


function 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.

}

?>
 

Link to comment
Share on other sites

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.

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.