Madhumitha
Madhumitha

Reputation: 3824

Insert query not working in php

I am new to php.I am inserting two integer values in my table using the following code.

<?php
include 'config.php';
$from = 1;
$to = 3;
$query  = "INSERT INTO 'friendrequests'('from','to') VALUES(1,3)";
echo $query;
$result = mysql_query($query);
echo $result;
// if($row = mysql_fetch_array($result, MYSQL_ASSOC))
{  
  if($result)
 {
    echo "1";
    //exit();
 }
 else
 {
    echo "0";
//    exit();
 }
}

but echo also not at all printing any values.just am getting 0 as response.Please help me to solve.Thanks in advance.

Upvotes: 2

Views: 164

Answers (2)

Robert
Robert

Reputation: 20286

You have syntax error table names should be put into `` marks.

$query  = "INSERT INTO `friendrequests` (`from`,`to`) VALUES(1,3)";

Also to check if query worked you should use mysql_affected_rows()(when you click on this link look at the red warning box and read it)

mysql_affected_rows() - Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query associated with link_identifier.

Consider changing your db engine to mysqli or pdo because mysql_* functions will be removed and they are depracated.

Upvotes: 0

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

Use ` around column_name and table_name rather than '(single quote)

$query  = "INSERT INTO `friendrequests`(`from`,`to`) VALUES(1,3)";

Upvotes: 3

Related Questions