Page 1 of 1

[fixed]simple post not working

Posted: Mon Sep 11, 2006 11:12 am
by Obadiah
ok...i was having trouble before with database retrieval so i decided to go through take a different route....however this time also i ran into a couple of problems....the program works very simply...wite some text into a text box hit insert and theoretically it adds that text to the database in a field called testField....after you add it you run the php code below to show it but this is all a 3 part process i will post all 3 parts

First the html

Code: Select all

<html>
<head>
<title>Insert Form</title>
</head>
<body>
<form action="insert_modified.php" method="post">
<p>text to add:<br>
<input type="text" name="test field" size="30">
<p><input type="submit" name="submit" value="Insert Record"></p></p>
</form>
</body>
</html>
short enogh eh?...now the part that actually will add our string to the database right(once again theoretically speaking)

Code: Select all

<?php
// open the connection
$conn = mysql_connect("somehost", "a user", "somepass");
// database in use
mysql_select_db("testDB",$conn);
// create the SQL statement
$sql = "INSERT INTO testTable values ('', '$_POST[testField]')";
//execute the SQL statement
if (mysql_query($sql, $conn)){
echo "record added!";
}else {
echo "something went wrong";
}
?>


now comes the grand finaley...all your hard work will now show up...or so we hope

Code: Select all

<?php
// open the connection
$conn = mysql_connect("somehost", "a user", "somepass");
// database in use
mysql_select_db("testDB",$conn);
// create the SQL statement
$sql = "SELECT * FROM testTable";
//execute the SQL statement
$result= mysql_query($sql,$conn) or die(mysql_error());
// go through each row and display the result
while($newArray = mysql_fetch_array($result)){
// give a name to the fields
$id = $newArray['id'];
$testField = $newArray['testField'];
// echo the results on screen
echo "the ID is $id and the text is $testField<br>";
}
?>
but it doesent....why...is their a tiny something ive left out anywhere....here is whats really killing me...i set the ID to auto increment and it shows up fine...but the string that you add when you hit insert on our html page does not even appear in the database....the testField is just blank....why is that?

edited
another wierd thing is if you ran this

Code: Select all

<?php
// open the connection
$conn = mysql_connect("somehost", "a user", "somepass");
// database in use
mysql_select_db("testDB",$conn);
// create the SQL statement
$sql = "INSERT INTO testTable values ('', 'some value')";
//execute the SQL statement
if (mysql_query($sql, $conn)){
echo "record added!";
}else {
echo "something went wrong";
}
?>
testField will show "some value" so that means the problem will have to be with the way im posting it right....but what am i doing incorrectly?

Posted: Mon Sep 11, 2006 11:27 am
by blackbeard
Try this for the insert code:

Code: Select all

<?php
// open the connection
$conn = mysql_connect("somehost", "a user", "somepass");
// database in use
mysql_select_db("testDB",$conn);
// create the SQL statement

$testString = mysql_real_escape_string($_POST['testField']);

$sql = "INSERT INTO testTable values ('', $testString)";
//execute the SQL statement
if (mysql_query($sql, $conn)){
echo "record added!";
}else {
echo "something went wrong";
}
?>
I've had problems in the past using $_POST inside a SQL query string, breaking it ou usually does the trick for me. Also, have you verified that the test string is actually making into the table?

Posted: Mon Sep 11, 2006 11:32 am
by ok
First of all, you named the input field (HTML) as "test field" instead of "testField" (like you did in your php code).
Secondly, this one is bad:

Code: Select all

$sql = "INSERT INTO testTable values ('', '$_POST[testField]')";
This one is good:

Code: Select all

$sql = "INSERT INTO testTable values ('', '".$_POST['testField']."')";

Posted: Mon Sep 11, 2006 11:34 am
by Obadiah
@blackbeard=it gives the "something went wrong" error

i made a edit in my original at the bottom which shows that if you manually enter a string it appears

@ok= i tried your code and it did the same as mine...i even fixed test field to testField...still no luck...im telling you guys the post method doesnt like me...lol