cannot insert into localhost(mysql) database using php [duplicate] - php

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

Related

HTML Form not submitting if there is a ' character within the data [duplicate]

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!";
}
?>

Values aren't being entered into the database [duplicate]

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.

Why is my table not updating in MySql? [duplicate]

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.

Mysqli_close() expects parameter 1 to be mysqli [duplicate]

This question already has answers here:
php warning: mysqli_close() expects parameter 1 to be mysqli
(2 answers)
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 5 years ago.
I am working on android app project and in this project, i need to connect my android app with PHP and MySQL everything is fine but I am getting an error in my user registration .php file error like this
Warning: mysqli_close() expects parameter 1 to be myself, null given
in C:\xampp\htdocs\panel\UserRegistration.php on line 35
I have tried many things but the error is still there.
Here is what I have tried:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
include 'DatabaseConfig.php';
$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
$id_name = $_POST['id'];
$U_name = $_POST['username'];
$password = $_POST['password'];
$cnic = $_POST['cnic'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$CheckSQL = "SELECT * FROM Users WHERE email='$email'";
$check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));
if(isset($check)){
echo 'Email Already Exist';
}
else{
$Sql_Query = "INSERT INTO Users (id,username,email,password_cnic,phone) values ('$id','$U_name','$email','$password','$cnic','$phone')";
if(mysqli_query($con,$Sql_Query))
{
echo 'Registration Successfully';
}
}
?>
https://secure.php.net/manual/fr/mysqli.close.php
You must do : mysqli_close($con);
:)
Pass Connection object in mysqli_close() function.
Like :
mysqli_close($con);

Android post values to MySQL with PHP [duplicate]

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

Categories