Jump to content

SQL error....


lAZLf

Recommended Posts

i was making a little PHP chat thing so i can view peoples comments... but it doesnt seem to work. here's the code for the code to insert stuff into data base:

 

PHP Code:

<?php
$mysqli = mysqli_connect("localhost", "XXXXXXX", "XXXXXXX", "XXXXXXX");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$sql = "INSERT INTO testTable (testField)
VALUES ('".$_POST["testfield"]."')";
$res = mysqli_query($mysqli, $sql);

if ($res === TRUE) {
echo "A record has been inserted.<br/> <a href='view.php'>click here</a> to view messages";
} else {
printf("Could not insert record: %s\n", mysqli_error($mysqli));
}

mysqli_close($mysqli);
}
?>

and here's the code to view the comments:

 

PHP Code:

<?php
$mysqli = mysqli_connect("localhost", "XXXXXXX", "XXXXXXX", "XXXXXXX");

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$sql = "SELECT * FROM testTable";
$res = mysqli_query($mysqli, $sql);

if ($res) {
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
$id = $newArray['id'];
$testField = $newArray['testfield'];
echo "The ID is ".$id." and the text is ".$testField."<br/>";
}
} else {
printf("Couldn't retrieve records: %s\,", mysqli_error($mysqli));
}

mysqli_free_result($res);
mysqli_close($mysqli);
}
?>

but when i view the PHP in the browser it doesn't show the message but there's no error message.... mind helping me out?

 

if you would like to see the result go to http://screenbreaker.com/view.php

Link to comment
https://forums.phpfreaks.com/topic/86958-sql-error/
Share on other sites

Your code

<?php

$mysqli = mysqli_connect("localhost", "XXXXXXX", "XXXXXXX", "XXXXXXX");

 

if (mysqli_connect_errno()) {

printf("Connect failed: %s\n", mysqli_connect_error());

exit();

} else {

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

$res = mysqli_query($mysqli, $sql)or die("Error message");

 

if ($res === TRUE) {

echo "A record has been inserted.<br/> <a href='view.php'>click here</a> to view messages";

} else {

printf("Could not insert record: %s\n", mysqli_error($mysqli));

}

 

mysqli_close($mysqli);

}

?>

and here's the code to view the comments:

 

PHP Code:

 

Code:

<?php

$mysqli = mysqli_connect("localhost", "XXXXXXX", "XXXXXXX", "XXXXXXX");

 

if (mysqli_connect_errno()) {

printf("Connect failed: %s\n", mysqli_connect_error());

exit();

} else {

$sql = "SELECT * FROM testTable";

$res = mysqli_query($mysqli, $sql);

 

if ($res) {

while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {

$id = $newArray['id'];

$testField = $newArray['testfield'];

echo "The ID is ".$id." and the text is ".$testField."<br/>";

}

} else {

printf("Couldn't retrieve records: %s\,", mysqli_error($mysqli));

}

 

mysqli_free_result($res);

mysqli_close($mysqli);

}

?>

 

LIsten :

1)  $_POST[testfield] use like this or  $_POST['testfield'] both are same

2) or die(""); i have used above.

3) or that is not good practise to use  $_POST['testfield'] in query it works but sometimes.

    you must use a variable with double quotations like this $var = $_POST['testfield'];

  ('$var');

 

TAke care

 

Link to comment
https://forums.phpfreaks.com/topic/86958-sql-error/#findComment-446766
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.