如何通过php向mysql(wamp)数据库中插入数据?
how to insert data in mysql( wamp) database through php?
试了很多次,连接成功,但是无法插入数据,请帮帮我
以下是我的代码,我刚刚添加了一个 if 检查,以查看查询是否没有向数据库添加任何数据然后它应该给出某种指示所以它说无法 post .当我在 wamp 数据库的数据库 table 中看到时,没有数据被插入或添加到 table,请帮助我,将不胜感激。谢谢。
<?php
if(isset($_POST['id']) && isset($_POST['name']) && isset($_POST['semester']) && isset($_POST['section'])):
$post_id = $_POST['id'];
$post_name = $_POST['name'];
$post_semester = $_POST['semester'];
$post_section = $_POST['section'];
$link = new mysqli('localhost','root','','registration');
if($link->connect_error)
die('connection error: '.$link->connect_error);
$sql = "INSERT INTO student_info(student_id, student_name, semester, section) VALUES('".$post_id."', '".$post_name."', '".$post_semester."', '".$post_section."',)";
//echo $sql;
$result = $link->query($sql);
if($result > 0):
echo 'Successfully posted';
else:
echo 'Unable to post';
endif;
$link->close();
die();
endif;
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Student Registration</h1>
<form action="add.php" method="post">
Student ID : <input type="text" name="id"/><br><br>
Student Name : <input type="text" name="name"/><br><br>
Semester : <input type="text" name="semester"/><br><br>
Section : <input type="text" name="section"/><br><br>
<input type="submit" value="create"/>
</form>
</body>
</html>
你的SQL末尾多了一个逗号(,)。正确的是 -
$sql = "INSERT INTO student_info(student_id, student_name, semester, section) VALUES('".$post_id."', '".$post_name."', '".$post_semester."', '".$post_section."')";
我还看到了您当前代码的错误 'Unable to Post',因此它工作正常。
尝试一下;
<?php
if(isset($_POST['id']) && isset($_POST['name']) && isset($_POST['semester']) && isset($_POST['section'])){
$post_id = $_POST['id'];
$post_name = $_POST['name'];
$post_semester = $_POST['semester'];
$post_section = $_POST['section'];
$link = new mysqli('localhost','root','','registration');
if($link->connect_error)
die('connection error: '.$link->connect_error);
$sql = "INSERT INTO student_info(student_id, student_name, semester, section) VALUES('$post_id', '$post_name', '$post_semester', '$post_section')";
//echo $sql;
$result = $link->query($sql);
if($result){
echo 'Successfully posted';
}else{
echo 'Unable to post';
}
$link->close();
die();
}
?>
试了很多次,连接成功,但是无法插入数据,请帮帮我
以下是我的代码,我刚刚添加了一个 if 检查,以查看查询是否没有向数据库添加任何数据然后它应该给出某种指示所以它说无法 post .当我在 wamp 数据库的数据库 table 中看到时,没有数据被插入或添加到 table,请帮助我,将不胜感激。谢谢。
<?php
if(isset($_POST['id']) && isset($_POST['name']) && isset($_POST['semester']) && isset($_POST['section'])):
$post_id = $_POST['id'];
$post_name = $_POST['name'];
$post_semester = $_POST['semester'];
$post_section = $_POST['section'];
$link = new mysqli('localhost','root','','registration');
if($link->connect_error)
die('connection error: '.$link->connect_error);
$sql = "INSERT INTO student_info(student_id, student_name, semester, section) VALUES('".$post_id."', '".$post_name."', '".$post_semester."', '".$post_section."',)";
//echo $sql;
$result = $link->query($sql);
if($result > 0):
echo 'Successfully posted';
else:
echo 'Unable to post';
endif;
$link->close();
die();
endif;
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Student Registration</h1>
<form action="add.php" method="post">
Student ID : <input type="text" name="id"/><br><br>
Student Name : <input type="text" name="name"/><br><br>
Semester : <input type="text" name="semester"/><br><br>
Section : <input type="text" name="section"/><br><br>
<input type="submit" value="create"/>
</form>
</body>
</html>
你的SQL末尾多了一个逗号(,)。正确的是 -
$sql = "INSERT INTO student_info(student_id, student_name, semester, section) VALUES('".$post_id."', '".$post_name."', '".$post_semester."', '".$post_section."')";
我还看到了您当前代码的错误 'Unable to Post',因此它工作正常。
尝试一下;
<?php
if(isset($_POST['id']) && isset($_POST['name']) && isset($_POST['semester']) && isset($_POST['section'])){
$post_id = $_POST['id'];
$post_name = $_POST['name'];
$post_semester = $_POST['semester'];
$post_section = $_POST['section'];
$link = new mysqli('localhost','root','','registration');
if($link->connect_error)
die('connection error: '.$link->connect_error);
$sql = "INSERT INTO student_info(student_id, student_name, semester, section) VALUES('$post_id', '$post_name', '$post_semester', '$post_section')";
//echo $sql;
$result = $link->query($sql);
if($result){
echo 'Successfully posted';
}else{
echo 'Unable to post';
}
$link->close();
die();
}
?>