實際上,我對此感到陌生,並且遇到了插入到mysql屬性的問題。無法在php中使用MYSQL INSERT INTO
我創建了兩個表格,即問題和答案。用戶可以通過html表單提交數據(對於兩個表)。由於一些未知的錯誤,數據沒有插入到答案表中,問題表沒有這樣的問題。 奇怪的是,兩個表的插入php代碼除了變量的名字幾乎相同。
singleques_info.php代碼:
<?PHP
session_start();
$t=$_GET['p'];
$ans= "";
$error_message = "";
$num_rows = 0;
function quote_smart($value, $handle)
{
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
if (!is_numeric($value))
{
$value = "'" . mysql_real_escape_string($value, $handle) . "'";
}
return $value;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$ans = $_POST['ans'];
$ans = htmlspecialchars($ans);
$uLength = strlen($ans);
if ($uLength <=100)
{
$error_message = "";
}
else
{
$error_message = $error_message . "Username must be between 10 and 20 characters" . "<BR>";
}
if ($error_message == "")
{
$username = "root";
$password = "";
$database = "techinsight";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $username, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found)
{
echo "connected";
$ans = quote_smart($ans, $db_handle);
$SQL = "SELECT * FROM answers WHERE answer= $ans";
$result = mysql_query($SQL);
$num_rows = mysql_num_rows($result);
if ($num_rows > 0)
{
echo "Question already taken";
}
else
}
$SQL = "INSERT INTO answers (Aid, answer, date) VALUES ($t, $ans, CURDATE())";
$result = mysql_query($SQL);
mysql_close($db_handle);
session_start();
$_SESSION['login'] = "1";
header ("Location:ques_wala_main.php");
echo "You are done.";
}
}
else
{
$errorMessage = "Database Not Found";
}
}
//<div class="content_mess">
/* else
{
session_start();
$_SESSION['login'] = "";
header ("Location: signup.php");
echo "Please SignIn Before Asking....";
}
// </div> */
}
?>
HTML表單:
<form id="ans" method="POST" name="ans" action="#">
<div class="field extra">
<label>your answer:</label> <br/> <br/> <br/>
<textarea name="ans" cols="70" rows="6"></textarea>
</div>
<br/> <br/>
<input type="submit" name="submit" value="submit" />
</form>
我也注意到你連接到DB ** **後,如果'($ ERROR_MESSAGE == 「」)'並在此之前訪問'mysql_real_escape_string($ value,$ handle)'。你上面連接到數據庫嗎? –
另外,使用'$ t = $ _ GET ['p'];'這是來自POST方法的GET方法。 '$ t = $ _ GET ['p'];'從哪裏填充? –
實際上,此處使用GET方法從通過url傳遞的另一個頁面檢索變量。 – shibu