iT邦幫忙

0

php Insert語法錯誤,但google完還是不知道錯在哪(unexpected '' (T_ENCAPSED.....

  • 分享至 

  • xImage

大大好,以下是我有錯誤的地方

   $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>
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
rogeryao
iT邦超人 8 級 ‧ 2020-11-01 12:29:45
最佳解答

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的寫法就正常,可是不懂為啥@@"

rogeryao iT邦超人 8 級 ‧ 2020-11-01 13:22:23 檢舉
$query = "INSERT INTO stud (stud_name) VALUES ('".$_POST["name"]."')";

試試,或是參考
PHP MySQL Insert Into

謝謝~貌似可以了...

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-11-01 15:49:29

如果你願意的話
改成這樣

$query = "INSERT INTO `stud` (`stud_name`) VALUES (".$_POST['name'].")";

或者

$query = "INSERT INTO `stud` (`stud_name`) VALUES ({$_POST['name']})";

就可以了

1
Franky Chen
iT邦研究生 3 級 ‧ 2020-11-01 20:57:12

有個東西叫SQL injection了解一下

我要發表回答

立即登入回答