大大好,以下是我有錯誤的地方
$query = "INSERT INTO `stud`(`stud_name`) VALUES ($_POST['name'])";
提示錯誤是:
'Expression' expected.php ']' expected.php syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
以上
附上原始碼:
<?php
include_once "../Connections/cnStu.php";
if (isset($_POST['submit'])) {
$query = "INSERT INTO `stud`(`stud_name`) VALUES ($_POST['name'])";
// $query = sprintf("INSERT INTO `stud`(`stud_name`) VALUES ('%s')",$_POST['name']);
echo $query;
$result = mysqli_query($link, $query);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
.wrap {
text-align: center;
}
label {
display: inline-block;
width: 100px;
text-align: right;
line-height: 30px;
}
</style>
</head>
<body>
<h1>新增學員資料</h1>
<div class="wrap">
<form id="form1" name="form1" method="post">
<p>
<label for="name">姓名:</label>
<input type="text" name="name" id="name" value="ggg">
<br>
<label for="idNo">身分證字號:</label>
<input type="text" name="idNo" id="idNo">
<br>
<label for="sex">性別:</label>
<input type="text" name="sex" id="sex">
<br>
<label for="birthday">生日:</label>
<input type="text" name="birthday" id="birthday">
<br>
<label for="school">畢業學校:</label>
<input type="text" name="school" id="school">
<br>
<label for="major">科系:</label>
<input type="text" name="major" id="major">
<br>
<label for="phone">行動電話:</label>
<input type="text" name="phone" id="phone">
<br>
<label for="addr">地址:</label>
<input type="text" name="addr" id="addr">
</p>
<p>
<input type="submit" name="submit" id="submit" value="送出">
<input type="reset" name="reset" id="reset" value="重設">
<br>
</p>
</form>
</div>
</body>
</html>
echo $query;
看看顯示什麼
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\student\results\add.php on line 5
然後我如果改用sprintf的寫法就正常,可是不懂為啥@@"
$query = "INSERT INTO stud (stud_name) VALUES ('".$_POST["name"]."')";
試試,或是參考
PHP MySQL Insert Into
謝謝~貌似可以了...
如果你願意的話
改成這樣
$query = "INSERT INTO `stud` (`stud_name`) VALUES (".$_POST['name'].")";
或者
$query = "INSERT INTO `stud` (`stud_name`) VALUES ({$_POST['name']})";
就可以了