简体   繁体   中英

How do I use PDO to submit form values to a database?

Ok so I have added a new form to my site to add sites. Currently I am using a system that uses PDO in a database.php file which is called from method in an adminprocess.php file, which itself is called from the page on submit.

Of course I am quite confused about how to do this. I have built the method in database.php which is below:

function addNewSite($name, $region, $address, $suburb, $state, $postcode, $contactFirst, $contactLast, $contactMob, $contactEmail){
          $query = "SELECT sitename, region, state FROM ".TBL_SITES." WHERE sitename = :name and region = :region and state = :state";
          $stmt = $this->connection->prepare($query);
          $stmt->execute(array(':name' => $name, ':region' => $region, ':state' => $state));
          $count = $stmt->rowCount();

          if(!$stmt || $count < 1){
            return 1; 
          }

         $query = "INSERT INTO ".TBL_SITES." SET sitename = :name, region = :region, streetaddress = :address, suburb = :suburb, state = :state, postcode = :postcode, contact_firstname = :contactFirst, contact_lastname = :contactLast, contact_mobile = :contactMob, contact_email = :contactEmail";
         $stmt = $this->connection->prepare($query);
         return $stmt->execute(array(':name' => $name, ':region' => $region, ':address' => $address, ':suburb' => $suburb, ':state' => $state, ':postcode' => $postcode, ':contactFirst' => $contactFirst, ':contactLast' => $contactLast, ':contactMob' => $contactMob, ':contactEmail' => $contactEmail));
       }

I then created the processing method in adminprocess.php which looks like:

 /* User submitted add site form */
      else if(isset($_POST['addsite'])){
         $this->procAddSite(); 

and

function procAddSite(){
      global $database;
      $database->addNewSite();

      header("Location: ".$session->referrer);
     }

and the form that I want to send is:

<form action="adminprocess.php" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr>
    <td>Name:</td>
    <td><input type="text" name="name" value="<?php echo $form->value("name"); ?>" /></td>
    <td><?php echo $form->error("name"); ?></td>
</tr>
<tr>
    <td>Region:</td>
    <td><input type="text" name="region" value="<?php echo $form->value("region"); ?>" /></td>
    <td><?php echo $form->error("region"); ?></td></tr>
<tr>
    <td>Street Address:</td>
    <td><input type="text" name="address" value="<?php echo $form->value("address"); ?>" /></td>
    <td><?php echo $form->error("address"); ?></td>
</tr>
<tr>
    <td>Suburb:</td>
    <td><input type="text" name="suburb" value="<?php echo $form->value("suburb"); ?>" /></td>
    <td><?php echo $form->error("suburb"); ?></td>
</tr>
<tr>
    <td>State:</td>
    <td><input type="text" name="state" value="<?php echo $form->value("state"); ?>" /></td>
    <td><?php echo $form->error("email"); ?></td>
</tr>
<tr>
    <td>Postcode:</td>
    <td><input type="text" name="postcode" value="<?php echo $form->value("postcode"); ?>" /></td>
    <td><?php echo $form->error("postcode"); ?></td>
</tr>
<tr>
    <td>Contact First Name:</td>
    <td><input type="text" name="contactFirst" value="<?php echo $form->value("contactFirst"); ?>" /></td>
    <td><?php echo $form->error("contactFirst"); ?></td>
</tr>
<tr>
    <td>Contact Last Name:</td>
    <td><input type="text" name="contactLast" value="<?php echo $form->value("contactLast"); ?>" /></td>
    <td><?php echo $form->error("contactLast"); ?></td>
</tr>
<tr>
    <td>Contact Mobile:</td>
    <td><input type="text" name="contactMob" value="<?php echo $form->value("contactMob"); ?>" /></td>
    <td><?php echo $form->error("contactMob"); ?></td>
</tr>
<tr>
    <td>Contact Email:</td>
    <td><input type="text" name="contactEmail" value="<?php echo $form->value("contactEmail"); ?>" /></td>
    <td><?php echo $form->error("contactEmail"); ?></td>
</tr>

<tr><td colspan="2" align="right">
<input type="hidden" name="addsite" value="1" />
<input type="submit" value="Add!" id="submit" /></td></tr>
<tr><td colspan="2" align="left">
    <?php echo "<a href=".$config['WEB_ROOT'].$config['home_page'].">Back to Home Page</a>"; ?>
</td></tr>
</table>

Now it all seems to be working, right up until it hits the database.php function and I end up getting some errors:

Warning: Missing argument 1 for MySQLDB::addNewSite(), called in /home7/ultima19/public_html/tesg/admin/adminprocess.php on line 256 and defined in /home7/ultima19/public_html/tesg/include/database.php on line 66

Warning: Missing argument 2 for MySQLDB::addNewSite(), called in /home7/ultima19/public_html/tesg/admin/adminprocess.php on line 256 and defined in /home7/ultima19/public_html/tesg/include/database.php on line 66

Warning: Missing argument 3 for MySQLDB::addNewSite(), called in /home7/ultima19/public_html/tesg/admin/adminprocess.php on line 256 and defined in /home7/ultima19/public_html/tesg/include/database.php on line 66

Warning: Missing argument 4 for MySQLDB::addNewSite(), called in /home7/ultima19/public_html/tesg/admin/adminprocess.php on line 256 and defined in /home7/ultima19/public_html/tesg/include/database.php on line 66

Warning: Missing argument 5 for MySQLDB::addNewSite(), called in /home7/ultima19/public_html/tesg/admin/adminprocess.php on line 256 and defined in /home7/ultima19/public_html/tesg/include/database.php on line 66

Warning: Missing argument 6 for MySQLDB::addNewSite(), called in /home7/ultima19/public_html/tesg/admin/adminprocess.php on line 256 and defined in /home7/ultima19/public_html/tesg/include/database.php on line 66

Warning: Missing argument 7 for MySQLDB::addNewSite(), called in /home7/ultima19/public_html/tesg/admin/adminprocess.php on line 256 and defined in /home7/ultima19/public_html/tesg/include/database.php on line 66

Warning: Missing argument 8 for MySQLDB::addNewSite(), called in /home7/ultima19/public_html/tesg/admin/adminprocess.php on line 256 and defined in /home7/ultima19/public_html/tesg/include/database.php on line 66

Warning: Missing argument 9 for MySQLDB::addNewSite(), called in /home7/ultima19/public_html/tesg/admin/adminprocess.php on line 256 and defined in /home7/ultima19/public_html/tesg/include/database.php on line 66

Warning: Missing argument 10 for MySQLDB::addNewSite(), called in /home7/ultima19/public_html/tesg/admin/adminprocess.php on line 256 and defined in /home7/ultima19/public_html/tesg/include/database.php on line 66

Warning: Cannot modify header information - headers already sent by (output started at /home7/ultima19/public_html/tesg/include/database.php:66) in /home7/ultima19/public_html/tesg/admin/adminprocess.php on line 258

now my question is, Where am I supposed to declare those arguments??

I'm sorry about the long winded question, But I am trying to learn this PDO stuff lol

You're getting the errors because the function addNewSite declared in database.php is expecting the variables $name, $region, $address, $suburb, $state, $postcode, $contactFirst, $contactLast, $contactMob, $contactEmail to be passed to it.

But in adminprocess.php where you call this function, you are not supplying any variables.

eg)

function procAddSite(){
  global $database;

  //no variables supplied!!!
  $database->addNewSite();

  header("Location: ".$session->referrer);
 }

You need to pass the variables from the form which you have submitted. These will be available in the $_POST array and referenced using the 'name' element have specified in your form.

You should be able to do something like this to fix your problem:

 function procAddSite(){
  global $database;

  //we have variables supplied now which were submitted from the form :)
  $database->addNewSite($_POST['name'], $_POST['region'], $_POST['address'], $_POST['suburb'], $_POST['state'], $_POST['postcode'], $_POST['contactFirst'], $_POST['contactLast'], $_POST['contactMob'], $_POST['contactEmail]);

  header("Location: ".$session->referrer);
 }

You could also just pass $_POST to function addNewSite and extract the $_POST array elements out in that function instead of passing them in as parameters.

Hope the above helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM