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') ");

    }
}
?>

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());
}

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

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.

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);


    }
if (isset($_POST[b1])){
    $t1=$_POST[t1];
    $t2=$_POST[t2];
    $t3=$_POST[f1];
    $date=time();

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


    }

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.

Nothing has changed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.