Yanal Shmilan
Yanal Shmilan

Reputation: 11

error posting to mysql

I'm new to php and this is my first script:

function tinyf_users_add($name,$password,$email,$isadmin)
    {
      global $tf_handle;
      // define area
      $name = trim($name);
      $password = trim($password);
      $email = trim($email);
      $isadmin = trim($isadmin);
      $n_email= mysql_real_escape_string (strip_tags($email),$tf_handle);
      $n_pass = md5($password);
      $n_name = mysql_real_escape_string (strip_tags($name),$tf_handle);
      $n_iseadmin = abs(intval($isadmin));

      // check if not empty data
      if ((empty($name)) || (empty($password)) || (empty($email)) || (empty ($isadmin)))
          return false;
      else if (!filter_var($n_email,FILTER_VALIDATE_EMAIL)) // check if valid email
          return false;
      else{
          $query = sprintf("INSERT INTO `users` (NULL, '%S', '%s', '%s', '%d')",$n_name,$n_pass,$n_email,$n_iseadmin);
          $qresult = mysql_query($query);
          if (!$qresult){
              return false;
          }
      else{
              return true;
          }
      }
    }

This is the add function but it's not working when i use the function for example

tinyf_users_add('yanal','ss','[email protected]',0);

is not working

db connect code

<?php
  $tf_host = 'localhost'; 
  $tf_dbname = 'tinyforum'; 
  $tf_username = 'root'; 
  $tf_password = 'root'; 
  $tf_handle = @mysql_connect($tf_host,$tf_username,$tf_password);   
  if (!$tf_handle) 
    die('conn filad ');
  $tf_db_result = mysql_select_db($tf_dbname); 
  if (!$tf_db_result){
    die ('filed');
  } 
  @mysql_query("SET NAMES 'utf8'"); 
  function tinyf_db_close()
  {
    global $tf_handle;
    @mysql_close($tf_handle);
  } 
?>

Upvotes: 1

Views: 100

Answers (3)

Krish R
Krish R

Reputation: 22711

Can you try this, You have missed to add values in query

$query = sprintf("INSERT INTO `users` (`name`,`password`,`email`,`isadmin`) values ('%s', '%s', '%s', '%d')",
                 $n_name,
                 $n_pass,
                 $n_email,
                 $n_iseadmin);
$qresult = mysql_query($query, $tf_handle) or die(mysql_error());

Upvotes: 0

Raggamuffin
Raggamuffin

Reputation: 1760

Your SQL query is wrong. You need to specify fields before the values:

Try this:

$query = sprintf("INSERT INTO `users` (`field1`,`field2`,`field3`,`field4`,`field5`) VALUES (NULL, '%S', '%s', '%s', '%d')",$n_name,$n_pass,$n_email,$n_iseadmin);

Upvotes: 0

Code Lღver
Code Lღver

Reputation: 15603

In your insert query you have forgot the Values clause add that:

$query = sprintf("INSERT INTO `users` VALUES (NULL, '%S', '%s', '%s', '%d')",$n_name,$n_pass,$n_email,$n_iseadmin);
                                     ^^^ add here

NOTE: don't use the mysql_* as it has been deprecate.

Upvotes: 2

Related Questions