PHP代码执行,但没有任何内容插入数据库

时间:2018-06-21 15:49:26

标签: php mysql

我创建了一个接收表,该表将存储用户的信息。但是这些值不会插入数据库中。 这是我与数据库和sql语句的连接,用于插入用户将要发布的值。     

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="l3tme1N@123"; // Mysql password 
$db_name="shipping_pro"; // Database name  
$tbl_name="receiver"; // Table name 

// Connect to server and select database.
$con = mysql_connect("$host", "$username", "$password")or die("cannot 
connect"); 
if(!$con){
die ('cannot connect to the server')
}
if (!mysql_select_db("$db_name", $con))
{
echo"no db selected";
}

if(isset($_POST['save']))
{
$name = $_POST['name'];
$cc = $_POST['cc'];
$sender = $_POST['sender-name'];
$address=$_POST['address'];
$tracker = $_POST['tracker'];
$code_phone = $_POST['code_phone'];
$phone = $_POST['phone'];
$code_phone1 = $_POST['code_phone1'];
$telefono = $_POST['telefono'];


$sql1="INSERT INTO 'receival' (name,idcard,sender-name,address,tracker, 
receiver-telnum, sender-telnum) VALUES  
('$name','$cc','$sender','$address', '$tracker', '$code_phone$phone', 
'$code_phone1$telefono')";
$result = mysql_query($con,$sql1);
} 
?>

html代码

这是我为接受用户输入而创建的表格。

我已经在这里和Google上查看了大多数解决方案,但仍然找不到任何解决方法

<form action="receive.php"  method="post" class="form-horizontal" data- 
parsley-validate novalidate >
<div class="form-group " id="gnombre">
<label for="office" class="col-sm-2 control-label">Name of Recipient</label>
<div class="col-sm-10">
<input type="text" class="form-control office" parsley-trigger="change" 
required name="name"  placeholder="Name of Recipient">
</div>                                  
</div>
<div class="form-group " id="gnombre">
<label for="officer_name" class="col-sm-2 control-label"><?php echo $CEDULA; 
?></label>
<div class="col-sm-10">
<input type="number" class="form-control officer_name" parsley- 
trigger="change" required name="cc"  placeholder="<?php echo $numbercedula; ? 
>">
</div>
</div>
<div class="form-group " id="gnombre">
<label for="officer_name" class="col-sm-2 control-label">Sender</label>
<div class="col-sm-10">
<input type="text" class="form-control officer_name" iparsley- 
trigger="change" required name="sender-name"  placeholder="Name of Sender">
</div>
</div>  
<div class="form-group" id="gapellido">
<label for="address" class="col-sm-2 control- 
label"><?php echo $direccion; ?></label>
<div class="col-sm-10">
<input type="text" class="form-control address" parsley-trigger="change" 
required name="address"   placeholder="Receiver Address">
                                </div>
                          </div>
                          <div class="form-group" id="gapellido">
                                <label for="address" class="col-sm-2 control-label">Tracker Number</label>
                                <div class="col-sm-10">
                                 <form name="form2" action="" method="">
                                                    <!--<strong><?php echo $codeproducto; ?></strong><br> -->
                                                    <input type="text" autofocus list="browsers" name="" autocomplete="off" class="form-control" required>
                                                    <datalist id="browsers">
                                                        <?php
                                                            $pa=mysql_query("SELECT tracking FROM  courier 
                                                            ");             
                                                            while($row=mysql_fetch_array($pa)){
                                                                echo '<option value="'.$row['tracking'].'">';
                                                            }
                                                        ?> 
                                                    </datalist>

                                </div>
                          </div>
                          <div class="form-group" id="ptelefonos">
                                <label for="address" class="col-sm-2 control-label"><?php echo $telefono; ?></label>
                                <div class="col-sm-4">
                                    <select type="number" class="form-control ph_no" parsley-trigger="change" required name="code_phone" placeholder="<?php echo $telefonocustomer2; ?>">

                                        <option data-countrycode="GH" value="233">Ghana (+233)</option>

                                    </select>                                                                       
                                </div>
                                <div class="col-sm-6">
                                    <input type="number" class="form-control ph_no" parsley-trigger="change" required name="phone" placeholder="Receiver's Phone Number">   
                                </div>
                            </div>  
                            <div class="form-group" id="ptelefonos">
                            <label for="address" class="col-sm-2 control-label"><?php echo $telefono; ?></label>                                
                                <div class="col-sm-4">
                                    <select type="number" class="form-control ph_no" parsley-trigger="change" required name="code_phone1" placeholder="Sender's phone Number">

                                        <option data-countrycode="GH" value="233">Ghana (+233)</option>

                                    </select>                                                                       
                                </div>
                                <div class="col-sm-6">
                                    <input type="number" class="form-control ph_no" parsley-trigger="change" required name="telefono" placeholder="Sender's phone Number">
                                </div>
                          </div>
                          <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i>
                        <?php echo $cerrar; ?></button>
                        <input class="btn btn-success" name="save" type="submit"  id="submit" value="Save">
                       </div>
                    </form>

2 个答案:

答案 0 :(得分:0)

尝试使用此代码,您需要将“ receival”替换为receival:

 $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password="l3tme1N@123"; // Mysql password 
    $db_name="shipping_pro"; // Database name  
$tbl_name="receiver"; // Table name 

// Connect to server and select database.
$con = mysql_connect("$host", "$username", "$password")or die("cannot 
connect"); 
if(!$con){
die ('cannot connect to the server')
}
if (!mysql_select_db("$db_name", $con))
{
echo"no db selected";
}

if(isset($_POST['save']))
{
$name = $_POST['name'];
$cc = $_POST['cc'];
$sender = $_POST['sender-name'];
$address=$_POST['address'];
$tracker = $_POST['tracker'];
$code_phone = $_POST['code_phone'];
$phone = $_POST['phone'];
$code_phone1 = $_POST['code_phone1'];
$telefono = $_POST['telefono'];


$sql1="INSERT INTO receival (name,idcard,sender-name,address,tracker, 
receiver-telnum, sender-telnum) VALUES('$name','$cc','$sender','$address', '$tracker', '$code_phone$phone', 
'$code_phone1$telefono')";
$result = mysql_query($con,$sql1);
} 
?>

答案 1 :(得分:0)

已经在注释中指出,您必须对已准备好的语句使用mysqli_ *或PDO。因此,我将看看您最琐碎的错误(假设这是唯一的错误)

您的插入查询有很多错误:值未正确插入,并且表名用引号引起来。 更改为:

$sql1="INSERT INTO receival (name,idcard,sender-name,address,tracker, 
receiver-telnum, sender-telnum) VALUES  
('$name','$cc','$sender','$address', '$tracker', '$code_phone.$phone', 
'$code_phone1.$telefono')";

这应该修复它并使插入工作正常。