简体   繁体   English

无法使用php将数据插入mysql数据库

[英]Can not insert data into mysql database with php

I am having trouble inserting data to my table via PHP. 我无法通过PHP将数据插入到表中。 The "cc_connect.php" is the file that connects the database. “ cc_connect.php”是连接数据库的文件。 The form is there but when I submit it, no data is added to my table. 表单在那里,但是当我提交它时,没有数据添加到我的表中。 I've followed several tutorials and matched their methods without success. 我遵循了一些教程,并成功地匹配了他们的方法。 Is something not set up in my db? 我的数据库中是否未设置某些内容?

the function $dbcon is associated with my connection 功能$ dbcon与我的连接关联

<form method="post" action="cc_registration.php">
<input type="hidden" name="submitted" value="true" />

    First Name: <input type="text" name="first_name" />
    Last Name: <input type="text" name="last_name" />

<br />
<input type="submit" value="submit" />

  <?php

   if(isset($_POST['submit'])) {

   include ('cc_connect.php');

   if (!$dbcon) {

   die("Can not Connect: " . mysql_error());

}

   mysql_select_db("cooperstown",$dbcon);

$sql = "INSERT INTO cobra_registration (first_name,last_name) VALUES ('$_POST[first_name]', '$_POST[last_name]')";

mysql_query($sql,$dbcon);



mysql_close($dbcon);

}

  ?>

$_POST['submit'] is never set because you are passing submitted . 从未设置$_POST['submit']因为您正在传递submitted

change: 更改:

<input type="hidden" name="submitted" value="true" />

to: 至:

<input type="hidden" name="submit" value="true" />

As a side note your current query can easily be hacked. 附带说明一下,您当前的查询很容易被黑。 Use Prepared statements instead like PDO or MysQLi, here is an example in PDO: 使用Prepared语句代替PDO或MysQLi,这是PDO中的一个示例:

$fName = isset($_POST['first_name']) ? $_POST['first_name'] : '';
$lName = isset($_POST['last_name']) ? $_POST['last_name'] : '';

if ($fName && $lName) {
   $stmt = $db->prepare('
      INSERT INTO cobra_registration (first_name,last_name) 
      VALUES (:fname, :lname)
   ');

   $stmt->bindParam(':fname', $fName, PDO::PARAM_STR);
   $stmt->bindParam(':lname', $lName, PDO::PARAM_STR);

   $res = $stmt->execute();

   if ($res) {
      echo 'Success';
   } else {
      echo 'Failure';
   }
}

The mysql_* functions are deprecated, and should no longer be used. mysql_ *函数已被弃用,不应再使用。 Look into mysqli or PDO . 查看mysqliPDO

IMPORTANT NOTE 重要的提示

This is WIDE open to SQL Injection attacks . 这对SQL注入攻击开放。 You should use prepared statements to protect against such attacks. 您应该使用准备好的语句来防止此类攻击。

GGio nailed his answer, it was the submitted , but checking for submit . GGio钉了他的答案,那是submitted ,但检查submit He also provided a PDO example, so I'll demonstrate the same thing in mysqli: 他还提供了一个PDO示例,因此我将在mysqli中演示同样的事情:

$firstName = isset($_POST['first_name']) ? $_POST['first_name'] : '';
$lastName = isset($_POST['last_name']) ? $_POST['last_name'] : '';

if ($firstName && $lastName) {
    $stmt = $mysqli->prepare("INSERT INTO cobra_registration (first_name,last_name) 
  VALUES (?, ?)"); 
    $stmt->bind_param("ss", $firstName, $lastName);
    $stmt->execute();  

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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