Viewed   91 times

I want to insert data from a form into a table but everytime i hit submit nothing happens, i checked and re-checked everything but i can't find the reason.

indextest1.php

<!DOCTYPE html>

<html>    
    <head>  
        <title></title>    
        <meta charset="UTF-8">    
        <meta name="author" content="">    
        <meta name="description" content="">    
        <meta name="keywords" content="">   
    </head>    
    <body>
        <form action="test1.php" method="post">
        <input type="text" name="name"><br>
        <input type="text" name="lastname"><br>
        <input type="radio" name="radio" value="one" checked> one<br>   
        <input type="radio" name="radio" value="two" > two<br>
        <select name="drop">   
            <option value="0" selected>0</option>    
            <option value="1">1</option>    
            <option value="2">2</option>    
            <option value="3">3</option>    
            <option value="4">4</option>    
            <option value="5">5</option>   
            <option value="6">6</option>    
            <option value="7">7</option>    
            <option value="8">8</option>    
        </select>
        <input type="checkbox" name="check" value="1"><br>    
        <input type="submit" value="Submit">   
        </form>    
    </body>   
</html>

test1.php

<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "db-test";

$connect = ("$servername, $username, $password, $dbname") or die ("ERROR DURING CONNECTION");

$name = $_POST["name"];
$lastname = $_POST["lastname"];
$radio = $_POST["radio"];
$drop = $_POST["drop"];
$check = $_POST["check"];

$sql_insert = mysql_query("INSERT INTO test-table (id, name, lastname, radio, drop, check) VALUES ('', '$name', '$lastname', '$radio', '$drop', '$check')");
        header("location: indextest1.php");

?>

Table values

Thanks in advance, i hope i provided all the info needed.

 Answers

5

Besides the other answer,

Firstly, this part of your query:

INSERT INTO test-table (id, name, lastname, radio, drop, check)

should read as

INSERT INTO `test-table` (id, name, lastname, radio, `drop`, `check`)

MySQL is interpreting your table as "test minus table". Use ticks, or rename it using an underscore test_table which is a safe seperator.

Then you're using two MySQL reserved words, "drop" and "check". Use ticks for those also.

Reference:

  • http://dev.mysql.com/doc/refman/5.6/en/keywords.html

See how MySQL is telling you where the syntax error begins?

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'drop, check)

and it would have shown you another one after that being

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'check)

Just to clarify, you are mixing MySQL APIs with

mysql_query("INSERT ...

which those mysql_ functions do not intermix with your present method. Use the same from connection to query

mysqli_query($connect, "INSERT ...

and of course the fact that you're open to SQL injection. Use a prepared statement:

  • https://en.wikipedia.org/wiki/Prepared_statement

You should also check for empty fields against your POST arrays such as !empty().

Sidenote: If an apostrophe or any other character is to be inserted that MySQL will complain about, then you must escape them. Either way, you should.

Plus, remember to add exit; after header. Otherwise and if you have more code below that, it may want to continue and execute.

header("location: indextest1.php");
exit;
Sunday, August 7, 2022
2
$str = "";

foreach ($_COOKIE as $key => $value)
{
    $str .= $key . ":" . $value . ",";
}

$str = substr($str, 0, -1);
Thursday, December 1, 2022
 
kcwu
 
2

why is there a questionmark before activatepoll?

data : ({
                    polltitle:nameVal,
                    answerone:optVal1,
                    answertwo:optVal2,
                    answerthree:optVal3,
                    $activatepoll:viewpoll
                }),

Further you should send the send the variable $_POST['submit']:

data : ({
                        polltitle:nameVal,
                        answerone:optVal1,
                        answertwo:optVal2,
                        answerthree:optVal3,
                        activatepoll:viewpoll,
                        submit: 'yeahhh'
                    }),
Thursday, December 15, 2022
 
4

MySQL has a different timeout than PHP. You could increase it in php.ini on the line mysql.connect_timeout = 14400. Also increase the default_socket_timeout = 14400

Note that if your PHP setting allow you to do an ini_set, you can also do as follows:

ini_set('mysql.connect_timeout', 14400);
ini_set('default_socket_timeout', 14400);
Thursday, December 22, 2022
 
plutor
 
4

Your SQL Statement

"DELETE FROM ID (ID) VALUES ('".$id."')"

is wrong.

It should be

DELETE FROM table_name
WHERE some_column=some_value;

. So, change your statement to

DELETE FROM ID WHERE ID='$id'

Suggestions

  • You should use POST method for action which will result in data edit.
  • You should check the input, make sure it did not contain SQL statement. A good way is to use $stuff = mysql_real_escape_string($_GET["stuff"]).
Wednesday, December 21, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :