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)
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
When I did insert into the database, it worked. But when I try Update into the Database it doesn't insert the value.
<?php
// Connecting to MySQL Database.
$con = mysqli_connect("localhost", "root", "****", "users");
// Getting the received JSON into $json variable.
$json = file_get_contents('php://input');
// decoding the received JSON and store into $obj variable.
$obj = json_decode($json,true);
$username = $obj['username'];
// Populate Student name from JSON $obj array and store into $S_Name.
$Q1 = $obj['q1'];
// Populate Student Class from JSON $obj array and store into $S_Class.
$Q2 = $obj['q2'];
// Populate Student Phone Number from JSON $obj array and store into $S_Phone_Number.
$Q3 = $obj['q3'];
// Populate Email from JSON $obj array and store into $S_Email.
$Q4 = $obj['q4'];
// Creating SQL query and insert the record into MySQL database table.
$Sql_Query = "UPDATE testuser SET q1= '$Q1', q2 = '$Q2', q3 = '$Q3', q4 = '$Q4' WHERE username = '$username'";
if(mysqli_query($con,$Sql_Query)){
// If the record inserted successfully then show the message.
$MSG = 'Record Successfully Inserted Into MySQL Database.' ;
// Converting the message into JSON format.
$json = json_encode($MSG);
// Echo the message.
echo $json ;
}
else{
echo 'Try Again';
}
mysqli_close($con);
?>
If there is anything wrong with this code, please let me know. I also want to say that I am a beginner in PHP and React Native.
Related
This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Closed 6 months ago.
So basically I have a working HTML Form that when filled out and submitted goes tomy SQL database, I have check multiple times and it works as should unless there is a ' character in the data being submitted. If there is a ' it simply won't submit the form. I don't know why this is and was hoping someone would know and potentionally help my resolve the problem so that that character can be used. I am using PHP to connect my HTMLform to my SQL server.
All the columns in my SQL Table are VARCHAR(no.)
EDIT:
This is the code on my php file.
<?php
// database connection code
// $con = mysqli_connect('localhost', 'database_user', 'database_password','database');
$con = mysqli_connect('localhost', 'root', '7520NHOj','db_connect');
// get the post records
$txtName = $_POST['txtName'];
$txtEmail = $_POST['txtEmail'];
$txtPhone = $_POST['txtPhone'];
$txtMessage = $_POST['txtMessage'];
// database insert SQL code
$sql = "INSERT INTO `tbl_contact` (`fldName`, `fldEmail`, `fldPhone`, `fldMessage`) VALUES ('$txtName', '$txtEmail', '$txtPhone', '$txtMessage')";
// insert in database
$rs = mysqli_query($con, $sql);
if($rs)
{
echo "Your Contact form has been submitted, we will get back to you as soon as possible!";
}
?>
Use mysqli_real_escape_string function for solving problem.
Use the following code.
<?php
// database connection code
// $con = mysqli_connect('localhost', 'database_user', 'database_password','database');
$con = mysqli_connect('localhost', 'root', '7520NHOj','db_connect');
// get the post records
$txtName = mysqli_real_escape_string($con,$_POST['txtName']);
$txtEmail = mysqli_real_escape_string($con,$_POST['txtEmail']);
$txtPhone = mysqli_real_escape_string($con,$_POST['txtPhone']);
$txtMessage = mysqli_real_escape_string($con,$_POST['txtMessage']);
// database insert SQL code
$sql = "INSERT INTO `tbl_contact` (`fldName`, `fldEmail`, `fldPhone`, `fldMessage`) VALUES ('$txtName', '$txtEmail', '$txtPhone', '$txtMessage')";
// insert in database
$rs = mysqli_query($con, $sql);
if($rs)
{
echo "Your Contact form has been submitted, we will get back to you as soon as possible!";
}
?>
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)
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
I can't insert the registration form data into my localhost database.
I am using xampp server, running mysql 5, php 7 and apache2. I've provided the code that can insert the form data into the database. The database is connected. I have also restarted the xampp server but it shows the same problem.
<?php
$active='Account';
include("includes/header.php");
?>
//html form with correct name values
<?php
if(isset($_POST['register'])){
$c_name = $_POST['c_name'];
$c_email = $_POST['c_email'];
$c_pass = $_POST['c_pass'];
$c_country = $_POST['c_country'];
$c_city = $_POST['c_city'];
$c_contact = $_POST['c_contact'];
$c_address = $_POST['c_address'];
$c_image = $_FILES['c_image']['name'];
$c_image_tmp = $_FILES['c_image']['tmp_name'];
move_uploaded_file($c_image_tmp,"customer/customer_images/$c_image");
$insert_customer = "insert into customers (customer_name,customer_email,customer_pass,customer_country,customer_city,customer_contact,customer_address,customer_image,customer_ip) values ('$c_name','$c_email','$c_pass','$c_country','$c_city','$c_contact','$c_address','$c_image','$c_ip')";
$result = mysqli_query($con,$insert_customer);
$sel_cart = "select * from cart where ip_add='$c_ip'";
$run_cart = mysqli_query($con,$sel_cart);
$check_cart = mysqli_num_rows($run_cart);
if($check_cart>0){
$_SESSION['customer_email']=$c_email;
echo "<script>alert('User is already present');</script>";
echo "<script>window.open('checkout.php','_self')</script>";
}else{
$_SESSION['customer_email']=$c_email;
echo "<script>alert('You have been Registered Sucessfully')</script>";
echo "<script>window.open('index.php','_self')</script>";
}
}
?>
The data should be inserted into the db when I refresh the phpmyadmin page.
Check $con variable where you define. Connection variable should be define on page and call with mysqli function.
$result = mysqli_query($con,$insert_customer);
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:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 8 years ago.
The error is
Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\posdef\index.php on line 18
Evidently, I perceive that the query returns an object resource, but how to convert it into a string. I am caught up in that problem. How to convert the object resource into string and store it in a variable?
This is the piece of code where I am trying to recover the data stored -
$con = mysqli_connect("localhost", "root", "");
mysqli_select_db($con, "new");
$mylogo = mysqli_query($con, "SELECT first FROM hello WHERE sno=6");
You got the data, now you just need to fetch it:
$con = mysqli_connect("localhost", "root", "");
mysqli_select_db($con, "new");
$mylogo = mysqli_query($con, "SELECT first FROM hello WHERE sno=6");
while ($row = $mylogo->fetch_assoc()) {
echo $row["first"];
}
Check out the docs, there are additional examples. mysqli_fetch_assoc
Once you run the query, you have to read the results. If you're only expecting ONE row back, you can do the following instead of a while loop:
$rslt = mysqli_query($con, "SELECT first FROM hello WHERE son=6");
$row = $result->fetch_assoc();
$mylogo = $row['first'];