有人能明白为什么每次我从数据库得到响应时都得到0作为回报吗?如果我在表格中输入数字5,我会得到0,5等等。我已经发布了相关的代码。
我希望有人能帮我。向朱莉问好
HTML:
<div class="content">
<p>Number</p>
<div class="form">
<fieldset>
<legend>Record Number</legend>
<form id="myForm" action="select.php" method="post">
<input type="number" name="numbervalue" min="1" max="2">
<button id="sub">Save</button>
</form>
</fieldset>
</div>
<span id="result"></span>
</div>JS
// Insert function for number
function clearInput() {
$("#myForm :input").each( function() {
$(this).val('');
});
}
$(document).ready(function(){
$("#sub").click( function(e) {
e.preventDefault(); // remove default action(submitting the form)
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info){ $("#result").html(info);
});
clearInput();
});
})
// Recieve data from database
$(document).ready(function() {
setInterval(function () {
$('#show').load('response.php')
}, 3000);
});Response.php
<?php
include('session.php');
$query = "SELECT numbers FROM numbertable";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
echo "{$row['numbers']} <br>";
} ;select.php
<?php
include('session.php');
// Insert To Database
$strSQL = "INSERT INTO numbertable(numbers) VALUES('" .$_POST["numbervalue"] . "')";
if(mysql_query("INSERT INTO numbertable VALUES('numbers')"))
echo "Insert Succesfull";
else
echo "Failed";
// The SQL statement is executed
mysql_query($strSQL) or die (mysql_error());
// Close the database connection
mysql_close();
?>session.php
<?php
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "root", "root");
// Selecting Database
$db = mysql_select_db("roulette_db", $connection);
session_start();// Starting Session
// Storing Session
$user_check=$_SESSION['login_user'];
// SQL Query To Fetch Complete Information Of User
$ses_sql=mysql_query("SELECT username FROM login WHERE username='$user_check'", $connection);
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){
mysql_close($connection); // Closing Connection
header('Location: index.php'); // Redirecting To Home Page
}
?>发布于 2015-12-11 22:14:29
0来自这一行:
if(mysql_query("INSERT INTO numbertable VALUES('numbers')"))字符串'numbers'被转换为数字0,并将其插入到表中。然后在$strSQL中执行查询,该查询插入用户提交的数字。然后response.php从表中检索所有行,因此它返回0和5。
将select.php更改为:
<?php
include('session.php');
// Insert To Database
$strSQL = "INSERT INTO numbertable(numbers) VALUES(" . intval($_POST["numbervalue"]) . ")";
if(mysql_query($strSQL)) {
echo "Insert Succesfull";
} else {
echo "Failed: " . mysql_error();
}
mysql_close();
?>https://stackoverflow.com/questions/34233029
复制相似问题