PHP syntax error for storing data into mysql db -


i have resolved issue. following code works perfectly. thank all.

please relevant section of dbcontroller.php file follows:

<?php class dbcontroller {   function runquery2($query) {     $result = mysql_query($query);             return $result;     }   } 

in addition, have amended original mysql statements in main html/php file this:

<?php  session_start();  require_once("dbcontroller.php");  $db_handle = new dbcontroller();   if(!empty($_post["submit"])) {    if ($db_handle->runquery2("insert cquestionstable    (postid, ccode,    nick, queries) values ( 1,'cc-001', 'james', 'what        problem?')") === true) {     echo "new record created successfully";      } else {     echo "error in posting question, pls try again." . "<br>";    }  ?> 

thanks n cheers.

your code:

<?php     session_start();     require_once("dbcontroller.php");     $db_handle = new dbcontroller();      if(!empty($_post["submit"])) {          $sql = "insert cquestionstable (postid, ccode, nick, queries) values ( 1,'cc-001', 'james', 'what problem?')";         if ($db_handle->runquery($sql) === true) {             echo "new record created successfully";         } else {             echo "error: " . $**sql . "<br>" . $db_handle->error;         }     } ?> 

i see errors already.

first if (!empty($_post["submit"])) { should if (isset($_post["submit"])) {

then used if ($db_handle->runquery($sql) === true) { should if ($conn->query($sql) === true) {

then in echo used $**sql should $sql

then did not know dbcontroller.php final code should be

<?php     // session_start(); not need when inserting database     include "dbcontroller.php";      if (isset($_post["submit"])) {          $sql = "insert cquestionstable (postid, ccode, nick, queries) values (1, 'cc-001', 'james', 'what problem?')";         if ($conn->query($sql) === true) {             echo "new record created successfully";         } else {             echo "error: " . $sql . "<br>" . $connn->error;         }     } ?> 

dbcontroller.php

<?php  $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "mydb";  // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); }   ?> 

if still not understand why not working please @ w3schools

hope answer we're looking for.


Comments