4

The problem: insert.php connects fine, but only inserts empty values ('') when I hit 'save' on the html form. The text I type, which I'm trying to insert, isn't saved. Somewhere a connection isn't being made and that data is lost but I can't figure out exactly where. Any help?

HTML insert form (collecting data for 2 parameters, 'user' and 'thread')

<form action="insert.php" method="post">
user: <input type="text" name="user"><br>
thread: <input type="text" name="thread"><br>
<input type="submit" value="Save">
</form>

PHP code to connect to SQL, insert inputted values

<?php

$user = $_POST['user'];
$thread = $_POST['thread'];

$servername = "##.##.###";
$username = "harwoodjp";
$password = "~";
$dbname = "332";

//create connection
$conn = new mysqli($servername, $username, $password, $dbname);

//check connection
if ($conn->connect_error) {
    die("SQL (&#9746)<br/> " . $conn->connect_error);
}
echo "SQL (&#9745) <br/>";

$sql = mysql_connect($servername,$username,$password);
mysql_connect($servername,$username,$password);
mysql_select_db("332project");

//insert values
$insert_query = "INSERT INTO test1(user,thread) VALUES ('$user', '$thread')";
mysql_query($insert_query);

echo "<script>window.location='select.php'</script>"; //select.php displays the full table

//close MySQL
mysql_close($sql);
?>
2
  • 1
    Some generic remarks: it seems you connect three times (1x new mysqli, 2x mysql_connect). You also do not escape the form input. What would the insert query look like when I write the following in the user field: ', ''); DROP DATABASE DATABASE(); -- (hint: it will delete your entire database). Sanitize your inputs! :-) Commented Nov 30, 2014 at 0:08
  • Another remark: use header('Location: select.php'); die(); if you're just trying redirect. Use them as your last lines of code. Commented Nov 30, 2014 at 2:04

2 Answers 2

1

try this

<?php

$user = $_POST['user'];
$thread = $_POST['thread'];

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

//create connection
$conn = mysql($servername, $username, $password, $dbname);

//check connection
if ($conn->connect_error) {
    die("SQL (&#9746)<br/> " . $conn->connect_error);
}
echo "SQL (&#9745) <br/>";

$sql = mysql_connect($servername,$username,$password);
mysql_select_db("db");

//insert values
$insert_query = "INSERT INTO test1(user,thread) VALUES ('$user', '$thread')";
mysql_query($insert_query);

echo "<script>window.location='select.php'</script>"; //select.php displays the full table

//close MySQL
mysql_close($sql);

?>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for cleaning it up. Unfortunately, the problem persists, even with this code. $insert_query echoes 'INSERT INTO test1(user,thread) VALUES ('', '')', which means user and thread are still registering as empty values.
Hey, so now it's working for me. Mind if I ask what exactly you changed? Looks quite similar...
ya..sure..you write mysql_connect($servername,$username,$password) this line 2 times so mysql confuse to create connection & u use somewhere mysql_* and somewhere mysqli_*..so meaning is use any one of mysql_* or mysqli_*
0

It might be because the default form posting method is GET.

Either change your $_POST to $_GET or add method="POST" to your form tag.

7 Comments

Hi, thanks for the comment. Neither solution has worked. Guess the problem's elsewhere?
Post your revised version at the top, so we can see.
I honestly can't see anything wrong with what you have. You'll have to debug your code by pinpointing where it fails. I would print $user, $thread, $insert_query to see what values you're getting. (Don't forget to comment out <script> line.)
Next step: add print_r($_POST); as the first line. What do you get after submitting?
Unfortunately, I am out of ideas :( Try looking at similar questions on the site. There's bound to be one with a solution.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.