error occurred during data insert in mysql - php

my code -
require 'database.php';
$uid = $_SESSION['UserId'];
$title = $_POST['imgtitle'];
$tag = $_POST['imgtag'];
$date = date("d-m-y");
$q = "INSERT into album (`o_id`, `title`, `src`, `tag`, `date`)
VALUES('$uid', '$title', '$image_name', '$tag', '$date'";
$result = $mysqli->query($q) or die(mysqli_error($mysqli));
if ($result)
{
echo "<h1>File Uploaded Successfully! Upload more...!</h1>";
}
my database.php-
<?php
$db_name = "szdb";
$db_server = "localhost";
$db_user = "root";
$db_pass = "";
$mysqli = new MySQLi($db_server, $db_user, $db_pass, $db_name) or die(mysqli_error());
?>
ERROR-
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2

You are missing a closing bracket at the very end.
Change
$q = "INSERT into album (`o_id`, `title`, `src`, `tag`, `date`)
VALUES('$uid', '$title', '$image_name', '$tag', '$date'";
to
$q = "INSERT into album (`o_id`, `title`, `src`, `tag`, `date`)
VALUES('$uid', '$title', '$image_name', '$tag', '$date')";

make sure all of your POST variables are coming through correctly
$userid = isset($_POST['userid']) && !empty($_POST['userid']) ? $_POST['userid'] : null;

Related

PHP is not inserting Data

I have run it on the local server and it worked perfectly, but when I uploaded it to my web hosting, it stopped working.
Basically when I submit the form the browser just keeps loading and when I check the database no data was inserted.
I checked my database connection and I was able to connect it but can't get data from it.
This is my php:
<?php
$servername = "localhost";
$username = "itclubac_root";
$password = "*******";
$dbname = "itclubac_itclub";
$tnp = 0;
$name = $_POST['name'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$phone = $_POST['phone'];
$id = $_POST['id'];
$section = $_POST['section'];
$skills = $_POST['skills'];
$interests = $_POST['interests'];
$expectations = $_POST['expectations'];
$tnp = $_POST['tnp'];
$ip = $_SERVER['REMOTE_ADDR'];
if ( $tnp == 0 ) {
header('Location: ../../get_involved.php');
} else {
// Create connection
$con = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$query = mysqli_query($con, "SELECT * FROM member_registration WHERE email = '".$email. "'");
if ( mysqli_num_rows($query) > 0 ) {
header('Location: ../../get_involved.php?status=exist');
} else {
$query = mysqli_query($con, "SELECT * FROM member_registration WHERE college_id = '".$id. "'");
if ( mysqli_num_rows( $query) > 0 ) {
header('Location: ../../get_involved.php?status=exist');
} else {
$sql = "INSERT INTO member_registration (name, email, gender, phone_no, college_id, section, skills, interests, expectations, ip_address) VALUES ('$name', '$email', '$gender', '+880$phone', '$id', '$section', '$skills', '$interests', '$expectations', '$ip')";
if ($con->query($sql) === TRUE) {
header('Location: ../../get_involved.php?status=success');
}
}
}
}
$con->close();
?>
Edit
This is the site: Form Pagehttp://itclub.acc.edu.bd/get_involved.php if you register here, the page will just keep loading. However if you try to access the registration.php directly it sends you to the form page page as I said it to. When I tested it on local, it worked perfectly but after uploading to the host this problem is occurring.
I tried to sort your code and maybe the error is related on the of your query,
$sql = "INSERT INTO member_registration (name, email, gender, phone_no, college_id, section, skills, interests, expectations, ip_address) VALUES ('$name', '$email', '$gender', '+880$phone', '$id', '$section', $'skills', '$interests', '$expectations', '$ip')";
Have you notice this part $'skills' of the line? change your code into,
$sql = "INSERT INTO member_registration (name, email, gender, phone_no, college_id, section, skills, interests, expectations, ip_address) VALUES ('$name', '$email', '$gender', '+880$phone', '$id', '$section', '$skills', '$interests', '$expectations', '$ip')";
maybe it help.

Why does my code not insert the details to the database?

I have written the following code to insert data into my database table, but unfortunately, it doesn't work. I have double and triple-checked the code and there are no errors at all. The console also doesn't show any as well.
I have not misspelled any of the details required to connect to the database and the variables are also correct so I am at a loss.
If anyone could help it would be be deeply appreciated.
My code:
<?php
function obfuscate($type, $data) {
if ($type == "PIN"):
$f_int = (int)md5($data);
$data = str_split($data);
$rev = implode("", array_reverse($data));
$sum = array_sum($data) + $f_int;
$data = implode("", $data);
$data += $sum + $rev + 1026;
// Keep the first four digits if the result is longer
if (strlen($data) > 4):
$data = substr($data, 0, 4);
endif;
return $data;
elseif ($type == "password"):
$data = password_hash($data, PASSWORD_DEFAULT, ['cost' => 12]);
return $data;
endif;
}
function insert_user($username, $email, $password, $PIN, $Account_Type, $Account_Status, $Referrer, $Balance) {
// Connect to the server and the database or show error
$connection = mysqli_connect("localhost", "root", "") or die("Couldn't connect to the server.");
mysqli_select_db($connection, "Calisoft_flu_db") or die("Couldn't connect to the database.");
// Sanitise the data
$username = mysqli_real_escape_string($connection, $username);
$email = mysqli_real_escape_string($connection, $email);
$password = mysqli_real_escape_string($connection, $password);
$PIN = mysqli_real_escape_string($connection, $PIN);
// Get the rest of the data
$Registration_Date = date("Y-m-d");
// Obfuscate password and PIN
$password = obfuscate("password", $password);
$PIN = obfuscate("PIN", $PIN);
// Make query and insert data to database
$query = "INSERT INTO `users` (`ID`, `Username`, `Email`, `Password`, `PIN`, `Registration_Date`, `Account_Type`, `Account_Status`, `Referrer`, `Balance`) VALUES ('NULL', '$username', '$email', '$password', '$PIN', '$Registration_Date', '$Account_Type', '$Account_Status', '$Referrer', '$Balance')";
$registered = mysqli_query($connection, $query);
if ($registered) {
echo "Register successful!";
}
// End the connection
mysqli_close($connection);
}
insert_user("#user1", "user#gmail.com", "user12345678", "1234", "Member", "Active", "0", "0");
?>
You are inserting a string value of 'NULL' into your primary key, you either remove the quotes '' or omit ID from the query.
Remove Quotes:
$query = "INSERT INTO `users` (`ID`, `Username`, `Email`, `Password`, `PIN`, `Registration_Date`, `Account_Type`, `Account_Status`, `Referrer`, `Balance`) VALUES (NULL, '$username', '$email', '$password', '$PIN', '$Registration_Date', '$Account_Type', '$Account_Status', '$Referrer', '$Balance')";
Or Remove the ID column:
$query = "INSERT INTO `users` ( `Username`, `Email`, `Password`, `PIN`, `Registration_Date`, `Account_Type`, `Account_Status`, `Referrer`, `Balance`) VALUES ('$username', '$email', '$password', '$PIN', '$Registration_Date', '$Account_Type', '$Account_Status', '$Referrer', '$Balance')";

php error when submitting comment

I have an error in the php code for submitting a comment, the problem is the 4th line of code:
php_network_getaddresses: getaddrinfo failed: the requested name is
valid, but no data of the requested type was found.
Any ideas?
<?php
if( $_POST )
{
$con = mysql_connect('%', 'myuser', 'mypassword');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("_mysite", $link);
$users_name = $_POST['name'];
$users_email = $_POST['email'];
$users_website = $_POST['website'];
$users_comment = $_POST['comment'];
$users_name = mysql_real_escape_string($users_name);
$users_email = mysql_real_escape_string($users_email);
$users_website = mysql_real_escape_string($users_website);
$users_comment = mysql_real_escape_string($users_comment);
$articleid = $_GET['id'];
if( ! is_numeric($articleid) )
die('invalid article id');
$query = "
INSERT INTO `_mysite`.`comments` (`id`, `name`, `email`, `website`,
`comment`, `timestamp`, `articleid`) VALUES (NULL, '$users_name',
'$users_email', '$users_website', '$users_comment',
CURRENT_TIMESTAMP, '$articleid');";
mysql_query($query);
echo "<h2>Thank you for your Comment!</h2>";
mysql_close($con);
}
?>
The % is mysql's wildcard, when you use it for your user's allowed host, it means 'any host', in your case here it should probably be localhost.
Not related to your question, but if you're not aware of it you should consider moving away from mysql_ functions which are deprecated, mysqli is the recommended replacement.
Replace $link with $con in this line mysql_select_db("_mysite", $link);.
This is the first time i am seeing any server name as %. I don't have much idea about it. But, you can replace % with localhost or port number or IP Address
<?php
if($_POST)
{
$con = mysql_connect('localhost', 'myuser', 'mypassword');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("_mysite", $con);
$users_name = mysql_real_escape_string($_POST['name']);
$users_email = mysql_real_escape_string($_POST['email']);
$users_website = mysql_real_escape_string($_POST['website']);
$users_comment = mysql_real_escape_string($_POST['comment']);
$articleid = $_GET['id'];
if(!is_numeric($articleid))
die('invalid article id');
$query = "INSERT INTO `_mysite`.`comments` (`id`, `name`, `email`, `website`,`comment`, `timestamp`, `articleid`)
VALUES (NULL, '$users_name','$users_email', '$users_website', '$users_comment',CURRENT_TIMESTAMP, '$articleid')";
mysql_query($query);
echo "<h2>Thank you for your Comment!</h2>";
mysql_close($con);
}
?>
Please stop using mysql* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi.
Using MySqli
<?php
if($_POST)
{
$con = mysqli_connect("localhost", "myuser", "mypassword", "_mysite");
if (!$con) {
die('Could not connect: ' . mysqli_connect_error());
}
$users_name = mysqli_real_escape_string($con, $_POST['name']);
$users_email = mysqli_real_escape_string($con, $_POST['email']);
$users_website = mysqli_real_escape_string($con, $_POST['website']);
$users_comment = mysqli_real_escape_string($con, $_POST['comment']);
$articleid = $_GET['id'];
if(!is_numeric($articleid))
die('invalid article id');
$query = "INSERT INTO `_mysite`.`comments` (`id`, `name`, `email`, `website`,`comment`, `timestamp`, `articleid`)
VALUES (NULL, '$users_name','$users_email', '$users_website', '$users_comment',CURRENT_TIMESTAMP, '$articleid')";
mysqli_query($con,$query)
echo "<h2>Thank you for your Comment!</h2>";
mysqli_close($con);
}
?>
Refer php_network_getaddresses: getaddrinfo failed: for more details.

I try upload the user comment into my db. After I click submit, everything looks fine but I look in my db, there are no record.

I tried the tutorial from some website.
I try upload the user comment into my db. After I click submit, everything looks fine but I look in my db, there are no record.
this is the php code
$dbhost = 'localhost:8090';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if( $_POST ){
$con = mysql_connect("localhost","root","password");
}
if(!$con){ die('Could not find db');}
mysql_select_db("doctorapp", $con);
$ticket_user_fname = $_POST['fname'];
$ticket_user_lname = $_POST['lname'];
$ticket_email =$_POST['email'];
$ticket_hp =$_POST['contact_number'];
$ticket_content = $_POST['content'];
$ticket_category = $_POST['category'];
$query =" INSERT INTO `ticketing`(`ticket_id`, `fname`, `email`, `content`, `category`, `contact_number`, `lname`)
VALUES ('ticket_id','ticket_time','ticket_user_fname','ticket_email','ticket_conetnt','ticket_category','ticket_hp','ticket_lname') ";
mysql_query($query);
echo "<h3>We will rreply you as soon as possible. Thank You.</h3>";
mysql_close($con);
?>
I am new for php. thank you!
May be you forgot to put $ before variable
$query =" INSERT INTO `ticketing`(`ticket_id`, `fname`, `email`, `content`, `category`, `contact_number`, `lname`)
VALUES
('$ticket_id','$ticket_time','$ticket_user_fname','$ticket_email','$ticket_content',
'$ticket_category','$ticket_hp','$ticket_user_lname') ";
Make sure that you also defined $ticket_id and $ticket_time before using them and if the ticket_id is the primary key of the table then it would be auto increment for the Uniqueness and you don't need to insert it seperately.
try this:
$dbhost = 'localhost:8090';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Could not find db');;
mysql_select_db("doctorapp", $con);
$ticket_user_fname = $_POST['fname'];
$ticket_user_lname = $_POST['lname'];
$ticket_email =$_POST['email'];
$ticket_hp =$_POST['contact_number'];
$ticket_content = $_POST['content'];
$ticket_category = $_POST['category'];
$query =" INSERT INTO `ticketing`(`ticket_id`, `fname`, `email`, `content`, `category`, `contact_number`, `lname`)
VALUES ('$ticket_id','$ticket_time','$ticket_user_fname','$ticket_email','$ticket_conetnt','$ticket_category','$ticket_hp','$ticket_lname') ";
if(mysql_query($query, $conn)){
echo "<h3>We will rreply you as soon as possible. Thank You.</h3>";
}
?>
you forgot the $ in the insert record

Insert form data in one table and then update an enum value in a different table?

I am using a code to insert a users form data into the database, that bit works fine. However, i also want to run another query and update an enum value 'form2_completed?' from No to Yes. I have added in the update query and now for some reason the script is not working and says 'ERROR'
Can someone please show me where i am going wrong. thanks
<?php
session_start();
$db_hostname = 'localhost';
$db_database = 'hewden1';
$db_username = 'root';
$db_password = '';
$db_server = mysql_connect($db_hostname, $db_username, $db_password)
or die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
$cname = $_POST['cname'];
$creg = $_POST['creg'];
$address = $_POST['address'];
$post = $_POST['post'];
$contactn = $_POST['contactn'];
$contactt = $_POST['contactt'];
$email = $_POST['email'];
$vat = $_POST['vat'];
$ipaddress = $_SERVER["REMOTE_ADDR"];
$sql="INSERT INTO supplier_registration (company_name, company_reg_number, company_address, company_postcode, contact_name, contact_number, contact_email, company_vat_number, date_time, user_ip)
VALUES ('$cname', '$creg', '$address', '$post', '$contactn', '$contactt', '$email', '$vat', NOW(), '$ipaddress')";
$sql="UPDATE supplier_session SET form2_completed? = 'Yes' WHERE form2_completed? = 'No'";
$result = mysql_query($sql);
if($result){
$success = "<div class='success'></div>"; // use the $success
//encode the URL parameter as :
$success = urlencode($success);
header("Location: index.php?success=$success");
}else {
echo "ERROR";
}
?>
You are overwriting the variable $sql and not running INSERT. Try:
$sql="INSERT INTO supplier_registration (company_name, company_reg_number, company_address, company_postcode, contact_name, contact_number, contact_email, company_vat_number, date_time, user_ip)
VALUES ('$cname', '$creg', '$address', '$post', '$contactn', '$contactt', '$email', '$vat', NOW(), '$ipaddress')";
$result = mysql_query($sql);
$sql="UPDATE supplier_session SET form2_completed? = 'Yes' WHERE form2_completed? = 'No'";
$result = mysql_query($sql);
Please note that the method you have used is deprecated from php 5.5.0. so i suggest you consider mysqli or PDO. examples can be found in below php manual links
http://www.php.net/manual/en/mysqli.query.php
http://www.php.net/manual/en/pdo.query.php

Categories