【发布时间】:2015-01-22 14:18:21
【问题描述】:
我找到了一个非常好的代码,如何从这个网站添加和修改记录: http://www.killersites.com/community/index.php?/topic/3064-basic-php-system-view-edit-add-delete-records-with-mysqli/
当我想添加一条新记录时,我的屏幕上出现了这个错误:
错误:无法准备 SQL 语句。
错误在下面的第 43 行附近
1 NEW RECORD
2
3 */
4 // if the 'id' variable is not set in the URL, we must be creating a new record
5 else
6 {
7 // if the form's submit button is clicked, we need to process the form
8 if (isset($_POST['submit']))
9 {
10 // get the form data
11 $titel = htmlentities($_POST['titel'], ENT_QUOTES);
12 $datum = htmlentities($_POST['datum'], ENT_QUOTES);
13 $omschrijving = htmlentities($_POST['omschrijving'], ENT_QUOTES);
14 $begin = htmlentities($_POST['begin'], ENT_QUOTES);
15 $eind = htmlentities($_POST['eind'], ENT_QUOTES);
16
17 // check that firstname and lastname are both not empty
18 if ($titel == '' || $datum == '' || $omschrijving == '' || $begin == '' ||
19 $eind == '')
20 {
21 // if they are empty, show an error message and display the form
22 $error = 'ERROR: Please fill in all required fields!';
23 renderForm($titel == '' || $datum == '' || $omschrijving == '' || $begin == 24 '' || $eind == '');
25 }
26 else
27 {
28 // insert the new record into the database
29 if ($stmt = $mysqli->prepare("INSERT famevents (titel, datum, omschrijving, 30 begin, eind) VALUES (?, ?)"))
31 {
32 $stmt->bind_param("ss", $titel, $datum, $omschrijving, $begin, $eind);
33 $stmt->execute();
34 $stmt->close();
35 }
36 // show an error if the query has an error
37 else
38 {
39 echo "ERROR: Could not prepare SQL statement.";
40 }
41
42 // redirec the user
43 header("Location: beheerevents.php");
44 }
45
46 }
47 // if the form hasn't been submitted yet, show the form
48 else
49 {
50 renderForm();
51 }
52 }
53
54 // close the mysqli connection
55 $mysqli->close();
就个人而言,我看不出我在这里做错了什么或出了什么问题.....
在此代码上还有一个 $_session 将检查某人是否登录。
我在做什么:
1.将代码修改为我需要从我的数据库中获取或发送到的数据
2. 我首先尝试让<textarea> 在<form> 中工作,但我做不到,所以我将其改回仅文本输入。仍然是我希望看到的与我目前拥有的代码一起工作的东西。
3.然后我看到我忘记修改我在数据库表中添加新记录的部分中的代码,就像上面代码的编辑部分一样。
在那之后,错误出现在我的屏幕上.....我有点纠结于如何解决这个问题...... 希望你能帮我解决这个问题..
【问题讨论】:
-
所以你得到了
echo "ERROR: Could not prepare SQL statement.";。这是你应该寻找的。在那里打印出$stmt->error()。它会告诉您 SQL 参数计数不匹配的情况。 -
好的,现在我明白了:致命错误:在 /public/sites/www.josdenhertog.nl/familiedenhertog/admin/famevents/records 中的非对象上调用成员函数 execute()第 200 行的 .php 可能是因为我在后台运行了一个会话来检查管理员是否是网站的成员?
-
Em,然后使用
$mysqli->。 -
好的,现在它显示:错误:列计数与第 1 行的值计数不匹配。警告:无法修改标头信息 - 标头已由(输出开始于 /public/ sites/www.josdenhertog.nl/familiedenhertog/admin/famevents/records.php:201)在 /public/sites/www.josdenhertog.nl/familiedenhertog/admin/famevents/records.php 第 206 行跨度>
-
第一句有效。你读过它吗?你看过你的 SQL 查询了吗?