doukou1718 2014-02-20 05:52
浏览 106
已采纳

如何使用PDO将表单值提交到数据库?

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

  • 写回答

1条回答 默认 最新

  • doufei9805 2014-02-20 06:16
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀