lAZLf Posted January 20, 2008 Share Posted January 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/86958-sql-error/ Share on other sites More sharing options...
Tandem Posted January 20, 2008 Share Posted January 20, 2008 Sorry, i wrote something completely wrong, disregard this post. Quote Link to comment https://forums.phpfreaks.com/topic/86958-sql-error/#findComment-444617 Share on other sites More sharing options...
Tandem Posted January 20, 2008 Share Posted January 20, 2008 Oops, i think i might have been correct after all. I think you need to choose which database to perform your queries on. Try putting: mysql_select_db("DB NAME HERE", $mysqli); after your connection function. Quote Link to comment https://forums.phpfreaks.com/topic/86958-sql-error/#findComment-444621 Share on other sites More sharing options...
revraz Posted January 20, 2008 Share Posted January 20, 2008 You should put some mysql_error statements after your queries so you would actually get an error returned. Quote Link to comment https://forums.phpfreaks.com/topic/86958-sql-error/#findComment-444622 Share on other sites More sharing options...
hamza Posted January 23, 2008 Share Posted January 23, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/86958-sql-error/#findComment-446766 Share on other sites More sharing options...
lAZLf Posted January 23, 2008 Author Share Posted January 23, 2008 that still doesn't work.... ??? screenbreaker.com/view.php Quote Link to comment https://forums.phpfreaks.com/topic/86958-sql-error/#findComment-447024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.