使用php将数据插入数据库

时间:2017-04-18 05:02:15

标签: php mysql database

<?php
$con = mysqli_connect("localhost", "root", "" , "prosports1");
if(!isset($con)) {
die("Connection failed !!!");
}
if(isset($_POST['send'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$query = "INSERT INTO review VALUES 
    ('','$name','$email','$contact','$subject','$message')";
$res = mysqli_query($con,$query);
if(isset($res)) {
//header("Location: homepage.php");
}
}
?>

我使用此代码将数据插入数据库[phpmyadmin] 但它不起作用 怎么做

4 个答案:

答案 0 :(得分:0)

INSERT到TABLE(column1,column2,...)VALUES(value1,value2,...)

答案 1 :(得分:0)

使用此

if ($con->query($query) === TRUE) {
    echo "Record Successfully<br>";
}

答案 2 :(得分:0)

以下是POST方法的示例代码。它对我有用。遵循该模式,您的代码没有太大的变化。

<form action="new.php" method="post">
ID:<input type="text" name="id">
Name:<input type="text" name="name">
age:<input type="text" name="age">
place:<input type="text" name="city">
<input type="submit" value="submit" name="submit">
</form>

<?php
if(isset($_POST['submit']))
{
    $con = mysqli_connect("localhost", "username", "password","db_name");
$sql = "INSERT INTO employee VALUES ('$_POST[id]','$_POST[name]','$_POST[age]','$_POST[city]')";
mysqli_query($con,$sql);
echo "Data Inserted<br>";
mysqli_close($con);
}
?>

不要忘记更改操作页面和基本详细信息。欢呼声。

答案 3 :(得分:0)

以这种方式编写查询。

$query = "INSERT INTO review (column1 , column2 , column3 , column4 , column5 , column6) VALUES ('','$name','$email','$contact','$subject','$message')";
if(isset($res)) {
//header("Location: homepage.php");
}

用表属性替换列名。