MYSQL数据库在网页中插入和显示数据

时间:2016-08-14 13:58:23

标签: php mysql mysqli

我有一个提交后提交到MySQL数据库的表单,它运行正常。我有一个名为“customer_id”的字段,我希望将这三个字段组合起来设置一个值,一个是“first_name”,第二个是“last_name” “,第三个是”id“,例如customer_id =(first_name + last_name + id)value(johndoe1)。 Id字段不在表单中,这仅在具有自动增量的数据库中。在以上述方式设置costumer_id值之后,我想在提交表单后显示costumer_id值和成功消息。哈哈我想但不知道怎么做。 我怎么能这样做。

php code

<?php
error_reporting(-1);
//Establishing Connection with Server
$connection = mysqli_connect("host", "user", "pass","db");

    // Check connection
if (!$connection) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}    
if(isset($_POST['was'])){

//Fetching variables of the form which travels in URL

$first_name = mysqli_real_escape_string($connection, $_POST['first_name']);
$last_name = mysqli_real_escape_string($connection, $_POST['last_name']);
$pendant_name = mysqli_real_escape_string($connection, $_POST['pendant_name']);
$desired_language = mysqli_real_escape_string($connection, $_POST['desired_language']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$phone = mysqli_real_escape_string($connection, $_POST['phone']);
$address = mysqli_real_escape_string($connection, $_POST['address']);
$country = mysqli_real_escape_string($connection, $_POST['country']);
$state = mysqli_real_escape_string($connection, $_POST['state']);
$city = mysqli_real_escape_string($connection, $_POST['city']);
$zip = mysqli_real_escape_string($connection, $_POST['zip']);
$mobile = mysqli_real_escape_string($connection, $_POST['mobile']);

$sql =  mysqli_query($connection, "INSERT into order_request(customer_id, first_name, last_name, pendant_name, desired_language,  email, phone, mobile, address, country, state, city, zip) VALUES ('$first_name$last_name', '$first_name', '$last_name', '$pendant_name', '$desired_language', '$email','$phone', '$mobile', '$address', '$country', '$state', '$city', '$zip' )");
echo '<div class="success-msg">';
echo 'data successfully submitted!';
echo '<div class="close-btn">';
echo '</div>';
echo '</div>';
echo "
        <script type=\"text/javascript\">
        var c = document.querySelector('.close-btn');
        var e = document.querySelector('.success-msg');
        e.style.height = document.clientHeight+'px';
        c.onclick = function(){
        e.style.display = 'none';
        }
        </script>
    ";

}
 mysqli_close($connection);


?>

1 个答案:

答案 0 :(得分:0)

要获取ID,您需要使用mysqli_insert_id。只有在查询成功执行并返回最后一个插入ID时才会获得id。

还有一件事。请保持HTML和PHP代码分开。在PHP echo语句中编写HTML代码是一种非常糟糕的做法。

遵循以下模式:

<?php

 /* Other code */
 $sql =  mysqli_query(....) or die(mysqli_error($connection));
 if ($sql) {
?>
  <div class="success-msg">
     <?php echo 'Data successfully submitted!'; ?>
     <?php echo "Customer ID is : ".$first_name,$last_name.mysqli_insert_id($connection); ?>
     <div class="close-btn"></div>
  </div>
  <script type=\"text/javascript\">
    var c = document.querySelector('.close-btn');
    var e = document.querySelector('.success-msg');
    e.style.height = document.clientHeight+'px';
    c.onclick = function(){
    e.style.display = 'none';
    }
  </script>

<?php } ?>