user6265109 user6265109 - 1 year ago 50
MySQL Question

New row is created with empty values in php using mysql database

I am using ampps server for my database. Using php I have written script to connect with server database. Now I want to insert data in the database table.

I tried writing the script and new row is getting inserted with null values.

I tried multiple insert queries but did not succeed.



class insertUserHelper

private $name;
private $email;
private $status;

function insertUserHelper($name,$email,$status)
$this -> name = $name;
$this -> email = $email;
$this -> status = $status;


function insert()
$con = testDatabase::getDB();

echo $name;
echo $email;
echo $status;

$sql = "INSERT INTO user ". "(name,email,status) ".
"VALUES ". "('$name','$email','$status')";

if (mysqli_query($con, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);



Can anyone help please,very new to php. Thank you..

Answer Source

You are creating local variables $name, $email and $status in your insert() function, try using $this->name, $this->email and $this->status instead. And use a prepared statement with bind variables instead of building a vulnerable query like that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download