如何使用php将打印值存储在mysql表中

时间:2014-12-01 11:21:03

标签: php mysql

我使用get变量在地址栏中打印值,它工作得很好,然后我的问题是我想将打印的值存储到mysql表中,但它只存储空值,请帮助我在哪里出错在下面附上的代码。

<?php 
session_start();
include('header.php');
?>
<form name="cart.php">


<table>
<tr>
      <th> ID </th>
      <th> Vehicle Description </th>
      <th> Price </th>
</tr>
<tr>
      <td><?php echo $_GET['id']; ?> </td>
      <td><?php echo $_GET['name']; ?> </td>
      <td><?php echo $_GET['price']; ?> </td>
</tr>
</table>
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$x=$_GET['id'];
$y=$_GET['name'];
$z=$_GET['price'];

$sql = "INSERT INTO products (id,description,price)
VALUES ('$x', '$y', '$y')";
if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

<form><input type="button" value="Go back" onClick="window.location.href='automobile_list.php'"></form>
</form> 

<?php
include('footer.php');
?>

2 个答案:

答案 0 :(得分:0)

<?php 
session_start();
include('header.php');
$x=$_GET['id'];
$y=$_GET['name'];
$z=$_GET['price'];
?>
<form name="cart.php">


<table>
<tr>
      <th> ID </th>
      <th> Vehicle Description </th>
      <th> Price </th>
</tr>
<tr>
      <td><?php echo $x; ?> </td>
      <td><?php echo $y; ?> </td>
      <td><?php echo $z; ?> </td>
</tr>
</table>
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO products (id,description,price)
VALUES ('$x', '$y', '$y')";
if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

<form><input type="button" value="Go back" onClick="window.location.href='automobile_list.php'"></form>
</form> 

<?php
include('footer.php');
?>

使用上面的代码,您将获得代码。

答案 1 :(得分:0)

问题在于查询

所以将该行更新为:

$sql = "INSERT INTO products (id,description,price)
VALUES ('".$x."', '".$y."', '".$y."')";