从SQL返回放0

时间:2015-12-11 21:42:56

标签: php sql select return

每次我从数据库得到回复时,有谁可以看到我为什么得到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
    }
?>

1 个答案:

答案 0 :(得分:1)

0来自这一行:

    if(mysql_query("INSERT INTO numbertable VALUES('numbers')"))

字符串'numbers'将转换为数字0,并将其插入表中。然后它在$strSQL中执行查询,该查询插入用户提交的号码。然后response.php检索表中的所有行,因此它返回05

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();

?>