【问题标题】:php mysql form not inserting Dataphp mysql表单不插入数据
【发布时间】:2017-07-17 03:30:57
【问题描述】:

我有一个项目,但对未传递给数据库的值感到震惊。排除了仅为城市国家保留的 html 结构。我的代码显示在此处。请注意,我有国家/地区城市的单独表格。我正在使用 WAMP 服务器。

    ============Database conn=============
<?php

$db= mysqli_connect('localhost', 'root','' , 'test' );

if(!$db) {
    echo mysqli_error($db);
    return;
}

echo 'Connection OK';
?>

=============Table - cust ================
`SNo`, `Customer Id`, `Card`, `First Name`, `Last Name`, `Gender`, `DOB`, `Age`, `Mobile`, `Address`, `Email Id`, `C Type`, `RefrenceId`, `Country`, `State`, `City`, `entry_date`

==========================
<?php
      include_once('db.php');
      session_start();

if(isset($_POST['submit'])) {
    $CustomerID = mysqli_real_escape_string($db ,$_POST['cid']);
    $Card = mysqli_real_escape_string($db ,$_POST['ccode']);
    "
    "
    "
    "
    $City = mysqli_real_escape_string($db ,$_POST['city']);

    $sql = "INSERT INTO cust 
VALUES('','$CustomerID','$Card','$FirstName','$LastName','$Gender','$DOB','$Age','$Mobile','$Address','$EmailId','$Ctype','$RefrenceId','$Country','$State','$City','".date("Y-m-d")."')";

    $query  = mysqli_query($db,$sql);

    if(!$query)
           echo mysqli_error();
    else       
        echo "<script> alert('Data inserted Successfully'); 
  </script>"; header('location:ow.php'); 
 }
?>
===============================================
<?php
include_once('db.php');
$DB_host = 'localhost';
$DB_user = 'root';
$DB_pass = '';
$DB_name = 'db';

try
{
$DB_con = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
$DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
$e->getMessage();
}
?>

<form  action="new_1.php" method="post" name="a" onsubmit="return validateForm();"><table width="102%" border="0">
<thead>
    <input type="text" name="cid" class="text-input"  value="<?php echo $rw; ?>" AUTO_INCREMENT="on" readonly="true"  />
    <input type="text" name="ccode" class="text-input"  value="<?php echo $number; ?>"  readonly="readonly"/>
    <input type="text" name="fname"  id="fname" class="text-input" onchange="toTitleCase(this)" />
    <input type="text" name="lname" id="lname" class="text-input" onchange="toTitleCase(this)" />
    <select name="gen" >
        <option value="Male">Male</option>
        <option value="Female">Female</option>
      </select>

    <input type="text"  id="dob" name="dob" class="tcal tcalinput" onfocus="test();" onblur="setAge();" onkeyup="this.onblur();" onpaste="this.onblur();" oninput="this.onblur();" value="<?php echo date("Y-m-d"); ?>"/>
      <input type="text" name="mob1" class="text-input" maxlength="10" />
      <textarea  name="add"  style="height:22" ></textarea>
      <select id="ctype" name="ctype" style="width:36mm" >
<option value="Select">Select</option>
<option value="Student">Student</option>
<option value="Employee">Employee</option>
<option value="Doesn'tMatter">Doesn'tMatter</option>
<option value="Accounting,Banking,Finance">Accounting,Banking,Finance</option>
</select></td>
       <input type="text" id="refno" name="refno" class="text-input"/>
       <td>Country</td>
      <td><span class="desc1">
<select name="country" class="country">
<option selected="selected">--Select Country--</option>
<?php
$stmt = $DB_con->prepare("SELECT * FROM country");
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
        <option value="<?php echo $row['country_id']; ?>"><?php echo $row['country_name']; ?></option>
        <?php
} 
?>
</select></td>
<td>State</td>
      <td><span class="desc1">
<select name="state" class="state">
<option selected="selected">--Select State--</option>
</select></td></tr>
    <tr>
      <td>City</td>
      <td><span class="desc1">
<select name="city" class="city">
<option selected="selected">--Select City--</option>
</select> </td>        </tr> 

    <input type="submit" id="submit" name="submit" value="Submit"></td>

          </form>

【问题讨论】:

  • 请回显您的查询
  • @mayank 抱歉没有找到你
  • echo $sql 并尝试直接在mysql中运行。注意:在mysqli_error($db)中传递连接变量($db);例如,if(!$query) echo mysqli_error($db);
  • @Naga 抱歉,它不起作用
  • 显示 echo $sql 的输出。

标签: php mysql database mysqli pdo


【解决方案1】:

echo $sql 并尝试直接在 mysql 中运行。注意:在 mysqli_error($db) 中传递连接变量($db);例如,if(!$query) echo mysqli_error($db);

与,

'SNo' 是自动递增列,对吗?如果是这样,则使用以下类型的插入语句

$sql = "INSERT INTO cust (`Customer Id`, `Card`, `First Name`, `Last Name`, `Gender`, `DOB`, `Age`, `Mobile`, `Address`, `Email Id`, `C Type`, `RefrenceId`, `Country`, `State`, `City`, `entry_date`) VALUES('$CustomerID','$Card','$FirstName','$LastName','$Gender','$DOB','$Age','$Mobile','$Address','$EmailId','$Ctype','$RefrenceId','$Country','$State','$City','".date("Y-m-d")."')";`

【讨论】:

    【解决方案2】:
    $sql = "INSERT INTO cust  VALUES('','$CustomerID','$Card','$FirstName','$LastName','$Gender','$DOB','$Age','$Mobile','$Address','$EmailId','$Ctype','$RefrenceId','$Country','$State','$City','".date("Y-m-d")."')";
    

    在这一部分中,你不应该在所有 php 变量($country、$Address 等)中添加引号。 否则,您将传递文本 -'$something' 而不是 $something 的值

    这是一个例子。

    $sql = "INSERT INTO cust VALUES(" 
           .$Mobile 
           .")";
    

    【讨论】:

      猜你喜欢
      • 2014-04-04
      • 1970-01-01
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多