简体   繁体   中英

my sql query not giving any results

I am trying to make a sql query, but its not working out for me.

PHP CODE:

<?php 
    if(isset($_POST['gobtn'])) {

        mysql_select_db('sessions', $conn);
        $conn = mysql_connect('localhost', 'root', '');

        if(!$conn) {
            die('Cannot connect to the database');
        } else {

            $sesid = $_POST['sesid'];

            $sql = "INSERT INTO `sessions`.`sesinfo` (`id`) VALUES ('$sesid');";
            $retval = mysql_query($sql, $conn);

            if(!$retval){
                die('Cannot enetr into the database');
            } else {
                echo "Done bro!";
                mysql_close($conn);
            }

        }

    }
?>

And the HTML Part:

<form method="POST" action="<?php $_PHP_SELF ?>">
        <h1 style="height: 60px; width: 500px; color: Black; font-family: Times New Roman; text-align: center; font-style: italic; font-style: bold; font-size: 20px; margin-left: 45%;">
            Enter any session ID: <input type="text" name="session_id" id="sesid">
            <input type="submit" value="GO" name="go_button" id="gobtn">    
        </h1>       
    </form>

I created db named as 'sessions'. It contains two tables:

id and text

I want to add the value of textbox input entered by user to the id table. But its not stopping on die command, and not even echoing out the last command. Any help would be appreciated alot.

Thanks in advance.

Try This,

if(isset($_POST['go_button'])) {

$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('sessions', $conn);

    $sesid = $_POST['sesid'];

    $sql = "INSERT INTO `sessions`.`sesinfo` (id) VALUES ('$sesid')";
    $retval = mysql_query($sql, $conn);

    if(!$retval){
        die('Error!');
    } else {
        echo "Done bro!";
        mysql_close($conn);
    }
}     

If your id is integer then your query will be->

 $sql = "INSERT INTO `sessions`.`sesinfo` (id) VALUES ($sesid)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM