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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀