This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
How can I prevent SQL injection in PHP?
(27 answers)
Closed 4 years ago.
Having trouble getting the data to insert into Maria DB. While attempting this project I have used about every source I can think of and I'm resorting to posting a question online. Below is my code and the error it produces is "Fatal error: Call to undefined method mysqli::mysqli_prepare() in C:\xampp\htdocs\scripts\insert.php on line 12". Been mulling over this for a couple days and so far I have found nothing at all on youtube or stackoverflow.
<?php
//Set up credentials
$servername = "localhost";
$username = "root";
$password = "";
$database = "test";
$db = new PDO("mysql:host=localhost;dbname=test;","$username","$password");
// Create connection
$link = new mysqli($servername,$username,$password,$database) or die($mysqli->error);
$a = $_POST["a"];
$sql = "INSERT INTO ass (asdf) VALUES('$a')";
$result = $link->mysqli_prepare($sql);
$stmt = $database->mysqli_prepare($sql);
$stmt->execute(array($_POST['a']));
$stmt->close();
?>
Related
This question already has answers here:
How can I get useful error messages in PHP?
(41 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
I am currently trying to develop my A-level computer science project so I can document it, however I am stuck on how to get some of my code to be entered into my database - it was working but I added a few bits to it and it stopped working when I was pressing the 'submit' link, the code that connects and adds to my database is shown:
if(isset($_POST['quiz'])) {
$name = $_POST['name'];
$question = $_POST['question'];
$answer = $_POST['answer'];
$dbc = mysqli_connect("","","","") or die('Error connecting to MySQL server')
$query = "INSERT INTO Quiz(name, question, answer) VALUES('$name', '$question', '$answer')";
$result = mysqli_query($db, $query) or die('Error querying database.');
mysqli_close($db);
}
echo "1 record added";
?>
Obviously I have all the details in the $dbc to connect to my database, I am a beginner with PHP and I am trying to get my head around why it isn't working, any help would be appreciated.
This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
How can I prevent SQL injection in PHP?
(27 answers)
Closed 4 years ago.
I'm new to php and dealing with databases. I have accomplished sending data from one arduino sensor to the database using PHP and XAMPP. My problem is sending data from multiple sensors.
The PHP code in file "write_data_w2"
<?php
$dbusername = "w123";
$server = "localhost";
$dbconnect = mysqli_connect($server, $dbusername);
$dbselect = mysqli_select_db($dbconnect,"weather1");
$sql = "INSERT INTO weather1.weather (temperature, humidity, rain) VALUES ('".$_GET["temperature"].",".$_GET["humidity"].",".$_GET["rain"]."')";
mysqli_query($dbconnect, $sql);
?>
I'm not using a password for the user "w123".
I wanted to check everything and tried inserting some made up data through browser with
"http://localhost/write_data_w2.php?temperature=32&humidity=45&rain=N"
and nothing happens, no warnings, no errors, no data. The database stays empty.
The database is named "weather1" consists of 1 table named "weather" and 5 columns named: "id", "time", "temperature", "humidity", "rain".
Solved
As a user suggested I added the line:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
which displayed some errors that I then solved.
I also had to modify "$sql" a bit:
$sql = "INSERT INTO weather1.weather (temperature, humidity, rain) VALUES ('".$_GET['temperature']."', '".$_GET['humidity']."', '".$_GET['rain']."')";
Just a suggestion
You should avoid the user of var or $GET/POST value directly in sql you are at risk for sql injection anyway you should check for error adding a $mysqli_error meggage ..
$dbusername = "w123";
$server = "localhost";
$dbconnect = mysqli_connect($server, $dbusername);
$dbselect = mysqli_select_db($dbconnect,"weather1");
$sql = "INSERT INTO weather1.weather (temperature, humidity, rain) VALUES ('".$_GET["temperature"].",".$_GET["humidity"].",".$_GET["rain"]."')";
mysqli_query($dbconnect, $sql);
// for check the erro try add
if (!$mysqli_query(dbconnect, $sql)) {
printf("Errormessage: %s\n", $mysqli_error);
}
?>
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I'm having problems with insert details to mysql server.
This is the code (a simple one):
<?php
//Input posted data.
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$Date = $_POST["Date"];
$Mail = $_POST["Mail"];
$Pass = $_POST["Pass"];
// Create connection
$conn = mysqli_connect('localhost','root',"");
//Check if the connection was opened, if not prompt the error to the page.
if ($conn)
{
die('Could not connect: ' . mysql_error());
}
//Select the data base.
mysqli_select_db("club",$conn);
//Set the character set to utf-8 to allow hebrew.
mysqli_query("SET NAMES 'utf8'");
//SQL query - user Details
$sql = "INSERT INTO 'customers' (Fname, Lname, Mail, Date, Pass)
VALUES('$Fname','$Lname','$Mail','$Date','$Pass');
//Run SQL query
$results = mysqli_query($query) or die (mysql_error());
//Close the SQL connection.
mysqli_close($conn);
?>
I'm getting this error:
Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\Contact.php on line 36 <--- The last line
Can really use your help.
thanks in advance,
Jason.
$results=mysqli_query($sql);
you have given $query instead of $sql
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I am trying to link my MySQL database to my PHP code. My friend has the same code as me and is able to connect their database to their PHP code. I end up getting a error code and do not know where I am going wrong.
This is the code that I am using
<?php
$username="root";
$password="password";
$database="account";
$connect = mysql_connect('localhost', $username, $password);
//$db-select = mysql_select_db($database,$connect) or die("Unable to select database");
$user = $_GET['user'];
$pass = $_GET['pass'];
if(!$connect) {
die('eror');
}
$db = my_sql_select_db("account", $connect)
mysql_query($db, "INSERT INTO 'account', 'tbl_account' (Username, Password) VALUES ('$user', '$pass')";);
mysql_close($connect);
?>
This is the outcome:
Parse error: syntax error, unexpected 'mysql_query' (T_STRING) in C:\Apache24\htdocs\PHP\index.php on line 11
add semicolon at the end of first line and remove the extra semicolon in second line
$db = my_sql_select_db("account", $connect);
mysql_query($db,"INSERT INTO 'account','tbl_account' (Username,Password) VALUES ('$user','$pass')");
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I work on an Android project, in which I use AsyncTask to send nameValuePairs to webservice.
I use PHP code below to send data from application to database:
<?php
require_once('dbConnect.php');
$studentid = $_POST['name'];
$classid = $_POST['classid']
$studentid = $_POST['studentid'];
$start = $_POST['start'];
$end = $_POST['end'];
$startsig = $_POST['startsig'];
$endsig = $_POST['endsig'];
$sql = "insert into signature (studentid,classid,start,end,startsig,endsig) values ('$studentid','$classid','$start','$end','$startsig','$endsig')";
if(mysqli_query($con,$sql)){
echo 'success';
}
else{
echo 'failure';
}
mysqli_close($con);
?>
Values in dbConnect.php file are correct. Database schema looks like this.
I checked, application sends out data successfully, but when I try to access PHP file above from browser I get a HTTP 500 error.
What else should I check in this case?
There are two problems I noticed in your code you didn't put a semicolon after
$classid = $_POST['classid']
And also why are you assigning two different post values to same phpvariable $studentid = $_POST['name']; and $studentid = $_POST['studentid'];.Anyways $studentid will have the last assigned value $_POST['studentid']
Try debugging for more errors