1,105,399 Community Members

dont save in db

Member Avatar
hanan-kh
Light Poster
34 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi..The problem is that the data is not saved in the database
Knowing he was not shown to me any errors in the code, but does not store data in the database

<?
class myclass{



function __construct(){
    $conn=mysql_connect("localhost","root","1");
    mysql_select_db("society",$conn);
    }

function validlogin($t1,$t2)
{ $res = mysql_query("select * from admin where username = '$t1' and pass = '$t2'");
  $count = mysql_num_rows($res);

  if($count== 0)
  return false;
  else
  return true;
}

function addnews($t2,$t1,$f1,$date){
    mysql_query("insert into news(title,content,pic,date) values('$t1','$t2','$f1','$time') ");

    }
}
?>



$t1,$date,$newimg);


    }

 ?>

Knowing that he enters the database when they connect and check the user name and password

//////////////////////////

<?
session_start();
include("myclass.class.php");

function genRandomString() {
    $length = 10;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $string = "";    

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }

    return $string;
}

 ?>

/////////////////////////

 <?
class myclass{



function __construct(){
    $conn=mysql_connect("localhost","root","1");
    mysql_select_db("society",$conn);
    }

function validlogin($t1,$t2)
{ $res = mysql_query("select * from admin where username = '$t1' and pass = '$t2'");
  $count = mysql_num_rows($res);

  if($count== 0)
  return false;
  else
  return true;
}

function addnews($t2,$t1,$f1,$date){
    mysql_query("insert into news(title,content,pic,date) values('$t1','$t2','$f1','$time') ");

    }
}
?>
Member Avatar
pritaeas
mod_pritaeas
11,316 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

Your method:

function addnews($t2, $t1, $f1, $date) {
    mysql_query("insert into news (title, content, pic, date) values ('$t1', '$t2', '$f1', '$time') ");
}

Two things with this:

  1. You use $date as parameter, yet use $time in the query.
  2. Add error logging (at least while testing) to see if/what goes wrong.

Use this:

function addnews($t2, $t1, $f1, $date) {
    mysql_query("insert into news (title, content, pic, date) values ('$t1', '$t2', '$f1', '$date') ") or die(mysql_error());
}
Member Avatar
hanan-kh
Light Poster
34 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I did what I advised but nothing has changed still does not store data in the database did not show any errors

Member Avatar
pritaeas
mod_pritaeas
11,316 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

Well, where do you execute the method? The only thing I see above is the class code. I do not see any calls to it anywhere. Just including the class doesn't execute it's code.

Member Avatar
hanan-kh
Light Poster
34 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
if (isset($_POST[b1])){
    $t1=$_POST[t1];
    $t2=$_POST[t2];
    $t3=$_POST[f1];
    $date=time();



    $img = $_FILES['f1']['name'];
    $imgfile = $_FILES['f1']['tmp_name'];
    $ext = end(explode(".",$img));
    $newimg = genRandomString().".".$ext;
    copy($imgfile,"../images/".$newimg);

    ///////////////
    $m=new myclass();
    $m->addnews($t2,$t1,$date,$newimg);


    }
Member Avatar
hanan-kh
Light Poster
34 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
if (isset($_POST[b1])){
    $t1=$_POST[t1];
    $t2=$_POST[t2];
    $t3=$_POST[f1];
    $date=time();

    $m=new myclass();
    $m->addnews($t2,$t1,$date,$newimg);


    }
Member Avatar
pritaeas
mod_pritaeas
11,316 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

You pass the parameters in a different order than required by the method. The date should be the last parameter. Also make sure b1 is set, other wise the code is never executed.

I suggest you rename your variables to something more readable too.

Member Avatar
hanan-kh
Light Poster
34 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Nothing has changed

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: