php 解析错误:语法错误,意外的“文本”(T_STRING),需要“,”或“;”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25350897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse error: syntax error, unexpected 'text' (T_STRING), expecting ',' or ';'
提问by Leo Frost
I am having trouble getting this to work. I have been able to call data from a dropdown menu and place it into a table and have it actively update without reloading the page. I am now trying to get the database information that is called to appear within a text input field or another drop down menu.
我很难让它工作。我已经能够从下拉菜单中调用数据并将其放入表格中,并在不重新加载页面的情况下对其进行主动更新。我现在正在尝试获取被调用以显示在文本输入字段或另一个下拉菜单中的数据库信息。
So basically I have a drop down menu that will call up user information, I am trying to get that information that is called to appear within another form so I can update it. Here is the code I am working with;
所以基本上我有一个下拉菜单可以调用用户信息,我试图获取被调用的信息以显示在另一个表单中,以便我可以更新它。这是我正在使用的代码;
table7.php
表7.php
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtDisp").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtDisp").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser2.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form method="post" action="localhost/table7.php">
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<?php
$con=mysqli_connect("localhost","user","password","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM users");
while($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['id'] . "'>" . $row['uname'] . "</option>";
mysqli_close($con);
}
?>
</select>
</form>
<br>
<div id="txtDisp"><b>Person info will be listed here.</b></div>
</body>
</html>
getuser2.php
获取用户2.php
<?php
$q = intval($_GET['q']);
$con=mysqli_connect("localhost","user","password","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT * FROM users WHERE id = '".$q."'";
$result1 = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Username</th>
<th>E-Mail</th>
<th>Info 1</th>
</tr>";
echo "<form action="getuser2.php" method="post">";
while($row1 = mysqli_fetch_array($result1)) {
echo "<tr>";
echo "<td><input type="text" name="info1" value=" . $row1['uname'] . "></td>";
echo "<td>" . $row1['email'] . "</td>";
echo "<td>" . $row1['info1'] . "</td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
mysqli_close($con);
?>
Within the getuser2.php code if you strip out the form input section and replace it with a call for uname using the format directly below for email and info it will display the data called from the database in standard text format.
在 getuser2.php 代码中,如果您去掉表单输入部分,并使用下面的电子邮件和信息格式将其替换为对 uname 的调用,它将以标准文本格式显示从数据库调用的数据。
However, I am encountering this error:
但是,我遇到了这个错误:
Parse error: syntax error, unexpected 'text' (T_STRING), expecting ',' or ';' in C:\wamp\www\getuser2.php on line 25
解析错误:语法错误,意外的“文本”(T_STRING),需要“,”或“;” 在 C:\wamp\www\getuser2.php 第 25 行
回答by serakfalcon
The problem is the following:
问题如下:
echo "<td><input type="text" name="info1" value=" . $row1['uname'] . "></td>";
You have double quotes inside a double-quoted string. PHP doesn't know where the string ends.
双引号字符串中有双引号。PHP 不知道字符串在哪里结束。
An easy fix (since you're not using variables inside the string anyway) is to change the double quotes to single quotes:
一个简单的解决方法(因为你没有在字符串中使用变量)是将双引号更改为单引号:
echo '<td><input type="text" name="info1" value="' . $row1['uname'] . '"></td>';
回答by hex494D49
Update these lines as shown below
更新这些行,如下所示
$sql = "SELECT * FROM users WHERE id = " . $q . "";
// ...
echo "<table border='1'><tr><th>Username</th><th>E-Mail</th><th>Info 1</th></tr>";
// ...
echo "<form action=\"getuser2.php\" method=\"post\">";
// ...
while($row1 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
// ...
echo "<td><input type=\"text\" name=\"info1\" value=" . $row1['uname'] . "></td>";