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.
Related
This question already has answers here:
Correct way to use LIKE '%{$var}%' with prepared statements?
(1 answer)
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 19 days ago.
<?php
$search = $_POST['search'];
$server_name="localhost";
$user_name="root";
$password="";
$database="Product_Management";
$conn = mysqli_connect($server_name,$user_name,$password,$database);
if(!$conn){
die("Connection Failed : ".mysqli_connect_error());
}
$qry = "select * from Product where Product_Name like '%.$search.%' ";
$result = mysqli_query($conn,$qry);
while($row=mysqli_fetch_array($result)){
echo $row['Product_Name'];
}
mysqli_close($conn);
I tried all the ways of resolving this issue, but couldn't find the answer.
I am also trying to test by REST API
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:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
How can I prevent SQL injection in PHP?
(27 answers)
Closed 5 years ago.
I am trying to store form data into database using mysqli but it is generating query error my code is given below....
When ever I try to submit the database connection is generating.. the $_POST is working perfectly.. the error only generating by mysqli_query..
<?php
$name = $_POST["firstname"] . " " . $_POST["lastname"];
$email = $_POST["email"];
$happen = $_POST["whendidhappen"];
$howlong = $_POST["howlong"];
$howmany = $_POST["howmany"];
$describe = $_POST["describe"];
$whattheydid= $_POST["whattheydid"];
$seenmycat = $_POST["seenmycat"];
$anythingelse = $_POST["anythingelse"];
$dbc = mysqli_connect('localhost','root','','abductionreport')
or die('Database connection error');
$query = "INSERT INTO abductionform (firstname, lastname, email,whendidhappen, howlong, describe, whattheydid, seenmycat,anythingelse)VALUES('$name','$name','$email','$happen','$howlong', '$howmany','$describe','$whattheydid', '$seenmycat','$anythingelse')";
$result = mysqli_query($dbc,$query) or die ("Query Error");
mysqli_close($dbc);
?>
<h3>Aliens Abducted Me - Report an Abduction</h3>
<p>Thanks for Submiting the form.</p>
<?php
echo "$name it happend to you on $happen it take $howlong <br>";
echo "Number of aliens: $howmany<br>";
echo "Describe: $describe<br>";
echo "What they did to you: $whattheydid<br>";
echo "Have you seen my cat: $seenmycat<br>";
echo "Anything else : $anythingelse<br>";
echo "Your Email Address is : $email<br>";
?>
DESCRIBE is a mysql keyword. Wrap the column name in backticks. For that matter wrap your table and all columns in backticks. Always check that $_POST elements exist with isset () before trying to access them. Use mysqli prepared statements with placeholders for improved security. Always perform error checking BEFORE posting to SO.
You also have 9 columns and 10 values in your query - this will cause a failure every time.
This question already has answers here:
How do I get PHP errors to display?
(27 answers)
Closed 6 years ago.
So, I have some bug in this code that will not execute the query, it just says The localhost page isn’t working - localhost is currently unable to handle this request. And that error shows only when there's some bug in the code, so it won't execute it. I don't know what's the problem here, as I tried to connect to database and that's all ok. Problem lies in lines underneath the connect.php.
if($_POST) {
include('connection.php');
$id_broj = $_POST['id_num'];
$password = $_POST['password'];
$query=mysqli_query("SELECT id,id_broj,ime,prezime FROM zaposleni");
$result=mysqli_query($conn,$query);
$count=mysqli_num_rows($result);
$row=mysqli_fetch_array($result);
echo 'Passed';
}
Need to write only query in $query Variable as:
if($_POST) {
include('connection.php');
$id_broj = $_POST['id_num'];
$password = $_POST['password'];
$query="SELECT id,id_broj,ime,prezime FROM zaposleni";
$result=mysqli_query($conn,$query);
$count=mysqli_num_rows($result);
$row=mysqli_fetch_array($result);
echo 'Passed';
}