用于从POST表单向mySQL添加数据的php

时间:2013-10-04 07:15:59

标签: php mysql post

我一直在阅读此板上的每条评论4小时,以便对此代码进行故障排除。我希望有人可以在这里给菜鸟提供一些反馈。

<?php
//Join.php
define('DB_NAME', 'biblaunch');
define('DB_USER', 'xxxxxxx');
define('DB_PASSWORD', 'xxxxxxxxx');
define('DB_HOST', 'xxxxxxxxx.hostedresource.com');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

$value = $_POST['name'];
$value2 = $_POST['email'];
$value3 = $_POST['shirt'];


$sql = "INSERT INTO customer (name, email, shirt) VALUES ('$value', 
'$value2','$value3')";`

$result = mysql_query($sql);

mysql_close();
?>

我的HTTP / mainpage使用此表单结构:

<form id="joinForm" action="join.php" method="post" accept-charset="utf-8">
                       <fieldset>                       
                        <div class="row-fluid">
                        <div class="span12">
             <label class="no">Name</label>
                 <input name="name" placeholder="Name" type="text">
                        </div>
                        </div>
                                     <div class="row-fluid">
                         <div class="span12">
             <label class="no">Email</label>
             <input name="email" placeholder="Email" type="text">
                                    </div>
                                    </div>
                                 <div class="row-fluid">
                 <div class="span12">   
                     <div class="row-fluid">
    <select style="display: none;" name="shirt" class="selectpicker span12">

<option selected="selected">Select Preferred Shirt</option>

<option>Mens Style Shirt</option>       

<option>Ladies Style Shirt</option>                                            
<option>Youth/Child Shirt</option>
    </select>
                        </div>
                        </div>
                        </div>
<div class="formFoot">                     
<button type="submit" class="btn">Submit</button>

提前感谢您的所有帮助

5 个答案:

答案 0 :(得分:1)

删除

中的字符“`
$sql = "INSERT INTO customer (name, email, shirt) VALUES ('$value', 
'$value2','$value3')";`

再次测试

答案 1 :(得分:0)

你有额外的角色:

$sql = "INSERT INTO customer (name, email, shirt) VALUES ('$value', 
'$value2','$value3')";`

试试这个:

$sql = "INSERT INTO customer (name, email, shirt) VALUES ('$value', 
'$value2','$value3')";

此外,您很容易进行SQL注入。在real_escape_string

上使用$_POST

答案 2 :(得分:0)

你的代码中有一些Syntex问题我修改了它检查

     <?php
  //Join.php
 define('DB_NAME', 'biblaunch');
 define('DB_USER', 'xxxxxxx');
 define('DB_PASSWORD', 'xxxxxxxxx');
 define('DB_HOST', 'xxxxxxxxx.hostedresource.com');

  $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
 die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

  if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
   }

   $value = $_POST['name'];

  echo $value;

 echo '<br></br>';
 $value2 = $_POST['email'];

  echo $value2;
  $value3 = $_POST['shirt'];


  $sql = "INSERT INTO customer (name, email, shirt) VALUES      ('$value','$value2','$value3')";

  $result = mysql_query($sql);

  mysql_close();

 ?>

答案 3 :(得分:0)

您好我已经在我的电脑上尝试过您的脚本,并在查询中找到了额外的单引号。此外,您的选择输入隐藏在HTML文件中。

不要在实际项目中使用Mysql api作为已弃用的。请使用PDO或MYSQLi。

完整的工作示例:

<?php

//join.php
define('DB_NAME', 'test');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_HOST', 'localhost');


$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

$value      = $_POST['name'];
$value2     = $_POST['email'];
$value3     = $_POST['shirt'];


$sql = "INSERT INTO customer (name, email, shirt) VALUES ('$value', '$value2','$value3')";

$result = mysql_query($sql) or die(mysql_error());

mysql_close();


?>

<强> HTML:

<!doctype html>
<head>
    <title>Items</title>
</head>
<body>
    <form id="joinForm" action="join.php" method="post" accept-charset="utf-8">
        <fieldset>                       
            <div class="row-fluid">
                <div class="span12">
                    <label class="no">Name</label>
                    <input name="name" placeholder="Name" type="text">
                </div>
            </div>
            <div class="row-fluid">
                <div class="span12">
                    <label class="no">Email</label>
                    <input name="email" placeholder="Email" type="text">
                </div>
            </div>
            <div class="row-fluid">
                <div class="span12">   
                    <div class="row-fluid">
                        <select style="" name="shirt" class="selectpicker span12">

                            <option selected="selected">Select Preferred Shirt</option>
                            <option>Mens Style Shirt</option>       
                            <option>Ladies Style Shirt</option>                                            
                            <option>Youth/Child Shirt</option>
                        </select>
                    </div>
                </div>
            </div>

             <div class="formFoot">                     
                <button type="submit" class="btn">Submit</button>
            </div>
        </fieldset>

    </form>

</body>
</html>

答案 4 :(得分:-1)

首先在最后一行关闭表格标签

</form>

现在删除“查询结束时的符号”

以下是正确的替换它

$sql = "INSERT INTO customer (name, email, shirt) VALUES ('$value', 
'$value2','$value3')";

在查询结束时