-3

I have 2 php files. One of them creates a table and inserts data into MySql using php. The other php selects the table in the database and outputs the data from the table. The table is outputting 0 results as in there aren't any record being stored into the table.

Need help, Thanks!

//select from table
         
    $sql = "SELECT * FROM post02";
                $result = mysql_query($link, $sql);
    echo 'selecting table works';
    
    //output from table
   
    if (mysql_num_rows($result) > 0) {        
        while($row = mysql_fetch_assoc($result)) {
     
                    
                    echo "id: " . $row["id"]. " - Name: " . $row["title"]. " " . $row["content"]. ""  . $row["date"]."<br>";
                        
                        
                    }
    } else {
        echo "0 results";
    } 
   
// create a table


 $sql="create table post02(id INT(6) unsigned auto_increment primary key , title varchar(30) not null, content varchar(255) not null, date TIMESTAMP)";



 $result = mysql_query($sql);

 if (! $result ) {

     die ( 'Cant create table : '  .  mysql_error ());

 }

  echo  'Created a table successfuly' ;


//insert to table
 echo'insert table - initializing';

 if($_POST['submit']){
 $title = $_POST['title'];
 $content = $_POST['content'];
 $date = date('l jS \of F Y h:i:s A');
 }
 
 $sql = "INSERT INTO post02(title, content, date) VALUES ($title, $content,$date))";

 if($title =="" || $content=="" ){
     echo "please compelete your post!";
     return;
      
 }
 echo'insert table completed';

    
     mysql_query($db ,$sql);
    header("location: viewblog.php");
  • Your code for creating table & inserting records work? – manian Mar 28 '17 at 23:52
  • @ManivannanSadasivam I think so, well it should. When i created my table, i used echo to see if the table was created. echo 'Created a table successfully'. if my website is outputting that echo, then im assuming it is created and inserted correctly. – Timmay696969 Mar 28 '17 at 23:54
  • @Timmay696969 your assumption is not always right. sometimes it will say that but it wont mean it was actually created. – Nodir Rashidov Mar 28 '17 at 23:58
  • Timmay696969, you're not checking any condition to print the success message. So there is a possibility that it could be wrong – manian Mar 29 '17 at 00:01
  • I just done an if statement to check whether my table is created, i get an error, "Cant create table : Table 'post02' already exists". This means that the table is created. – Timmay696969 Mar 29 '17 at 00:07
  • 1
    FYI, [you shouldn't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](https://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which one is best for you. – John Conde Mar 29 '17 at 00:09
  • 1
    Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). – John Conde Mar 29 '17 at 00:10
  • 1
    You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Use [`mysql_error()`](http://php.net/manual/en/mysql.error.php) to get a detailed error message from the database. – John Conde Mar 29 '17 at 00:11
  • @John Conde, he did executed his query but after printing success message – manian Mar 29 '17 at 00:14
  • @JohnConde i know mysql is a bit outdated or isnt used much these days but i believe i have to use it for my university server which they provide a mysql server. – Timmay696969 Mar 29 '17 at 00:15
  • The first parameter for mysql_query should be the query $result = mysql_query($sql, $link); and try pdo or mysqli because mysql is deprecated. – mbouzahir Mar 29 '17 at 00:15
  • @Timmay696969, please try the answer below – manian Mar 29 '17 at 00:16
  • 1
    @Timmay696969 it's not mysql the server that's outdated it's mysql library for php that's deprecated. you should use pdo or mysqli. – mbouzahir Mar 29 '17 at 00:20
  • i have changed all the "mysql" to "mysqli". But i believe there may be something wrong with inserting the data into the table, but im not sure what it is. – Timmay696969 Mar 29 '17 at 00:27

2 Answers2

-2

mysql_query accepts query as first parameter & connection string as second parameter. Try interchanging in all the locations in your code. Or you can remove the link parameter

manian
  • 1,418
  • 2
  • 16
  • 32
-2

Why are these on your code "mysql_query($db ,$sql);" and " mysql_query($link, $sql);" mysql_query takes just one parameter

  • 1
    wrong: `mixed mysql_query ( string $query [, resource $link_identifier = NULL ] )` http://php.net/manual/en/function.mysql-query.php –  Mar 29 '17 at 00:07