MySQL Entry to Database Not Working - php

I'm going through a course on MySQL, and I'm learning how to make a user entry bit of code (email and password) where the info in the script will be put into the database on phpMyAdmin. I can't seem to get it to work? My code doesn't have any errors when I put it through an error checker. I'm also completely new to PHP and MySQL. I know it can find the database, because I can update existing data.
<?php
$link = mysqli_connect("host", "username", "password", "username");
if (mysqli_connect_error()) {
die ("There was an error connecting to the database");
}
$query = "INSERT INTO `users` (`email`, `password`) VALUES('email', 'password')";
mysqli_query($link, $query);
$query = "SELECT * FROM users";
if ($result = mysqli_query($link, $query)) {
$row = mysqli_fetch_array($result);
echo "Your email is ".$row[1]." and your password is ".$row[2];
}
?>

Created a refined version. Check it.
<?php
$link = mysqli_connect("host", "username", "password", "username");
if (mysqli_connect_error()) {
die ("There was an error connecting to the database");
}
$query = "INSERT INTO `users` (`email`, `password`) VALUES('email', 'password')";
$result = mysqli_query($link, $query);
if($result != false)
{
echo "The record has been successfully inserted.<br>";
}
else
{
echo "Error Occured in the INSERT query.<br>Error : ".mysqli_error($link);
}
$query = "SELECT * FROM users";
$result = mysqli_query($link, $query);
if($result != false)
{
echo mysqli_num_rows($result)." Records found.<br>";
while($rows = mysqli_fetch_array($result))
{
echo $rows["email"]."<br>";
}
}
else
{
echo "Error Occured in the SELECT query.<br>Error : ".mysqli_error($link);
}
mysqli_close($link);
?>

Update
It turns out I didn't set the auto_increment setting, therefore making the way I set up my database incorrect! He set up another database in the tutorials I was going through, and I found out that as he did it. Thank you everyone for the effort to help me solve my problem!

Why you don't try receiving them with php?
And simply make
$email= $POST['email']
$password= $POST['password']
And change the query to
$query = "INSERT INTO `users` (`email`, `password`) VALUES(" .$email. ", ". $password.")";

Related

adding data with PHP to the db is not working

I'm using MAMP an my PHP won't add data to the db
I have looked at other similar questions and done everything on them, yet it still is not working.
I am using MAMP and I've done everything right as far as I know but the problem is the same no matter what I do.
<?php
require_once('dbconnect.php');
$email = $_GET['email'];
$name = $_GET['name'];
$message = $_GET['message'];
$my_query = "";
$my_query = "select * from Users where email = '$email' ";
$my_query = "INSERT INTO Users (email, name, message) VALUES ('$email', '$name', '$message')";
$result = mysqli_query($connection, $my_query );
if($result)
{
echo "Successfully Sent!";
}
else
{
echo "<b>ERROR: unable to post </b>";
}
}
mysqli_close();
?>
Here use this code and and tell what is the error
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform a query, check for error
if (!mysqli_query($con,"INSERT INTO Users (email, name, message) VALUES ('$email', '$name', '$message')"))
{
echo("Error description: " . mysqli_error($con));
}
mysqli_close($con);
?>
if you dont get any error or blank value in db use your values like
values('".$email."', '".$name."', '".$message."')"
in db It will be executed like values('test.abc.com', 'abc', 'It is working')

PHP is not passing proper data to MYSQL

I am unable to pass proper data to MySQL I have no idea what is wrong done by me please help me out my code is as follow:
$con = mysqli_connect('localhost', 'root', '', 'database');
if($con) {
echo "we are connected";
} else {
die("connection failed");
}
if(isset($_POST['submit'])) {
// echo "Yeah it works";
$user = $_POST['username'];
$pass = $_POST['password'];
$query = "INSERT INTO users(username, password) VALUES ('$user', '$pass')";
$result = mysqli_query($con, $query);
if(!$result) {
die("Query failed");
}
}
When I run this code I'm getting 0 for username and 0 for password. whatever I type for username and password I get 0 in database.
Try this code, which also removes SQL injections.
$con = mysqli_connect('localhost', 'root', '', 'database');
if(! $con )
die('Unable to connect');
foreach(array('submit', 'username', 'password') as $arg)
if(! isset($_POST[$arg]) )
die('Missing argument(s)');
unset($arg);
http_response_code(200);
$username = $con->real_escape_string($_POST['username']);
$password = $con->real_escape_string($_POST['password']);
$query = "INSERT INTO users (username, password) VALUES ('{$username}', '{$password}')";
if(! mysqli_query($con, $query))
http_response_code(400);
If this code doesn't work, make sure that username and password from your table are varchar's not int's. (Because your script inserts a 0 value)

Submit Button To Update Query

I have researched a lot and I think my error is something super simple or very hard, either way, this is my first question on StackOverflow.
So when I run this:
$query="UPDATE `sales` SET `sales` = '40' WHERE `sales`.`id` = 1";
it updates my database just fine but now I try to put this under a submit button and it won't work?
<?php
$link = mysqli_connect("localhost", "root", "TESTTEST", "sales");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
mysqli_query($link, $query);
if ($_POST['update'])
{
echo 'Updating...';
$query="UPDATE `sales` SET `sales` = '40' WHERE `sales`.`id` = 1";
echo '<br>Successfully Updated';
} else {
echo 'Unsuccessful';
}
?>
It echoes successfully updated then I check back to the database and nothing changed... Hopefully, you could help me! Thank you for reading, James.
First PHP tag is there just not showing on blockquote.
move your mysqli_query under query like this.
$link = mysqli_connect("localhost", "root", "TESTTEST", "sales");
if (mysqli_connect_error()){
die("Could not connect to database");
}
if ($_POST['update']) {
echo 'Updating...';
$query="UPDATE `sales` SET `sales` = '40' WHERE `sales`.`id` = 1";
mysqli_query($link, $query);
echo '<br>Successfully Updated';
} else{
echo 'Unsuccessful';
}
firstly
$query="UPDATE `sales` SET `sales` = '40' WHERE `sales`.`id` = '1'; ";
with single quotes
Then the mysqli_query($link, $query); part.

php bcrypt 505 error

I am trying to use a simple hash for users emails and passwords.
But when I run the following php script that is called on an ajax request i fet a 505 error.
<?php
$user = json_decode(file_get_contents('php://input'));
$email = $user->email;
$pass = $user->pass;
$cpass = $user->cpass;
$ssid = $user->ssid;
$type = $user->type;
$date = $user->regtime;
$con = mysqli_connect("localhost", "", "", "");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$validateEmail = "SELECT `Email` FROM `newUsers` WHERE `Email` = '$email' ";
$newVar = password_hash($pass, PASSWORD_DEFAULT);
if ($result = mysqli_query($con,$validateEmail)) {
if ($result->num_rows == 0){
$sql = "INSERT INTO `newUsers`(`email`, `type`, `date`, `ssid`, `hashpass`) VALUES ('$email', '$type', '$date', '$ssid', '$newVar')";
mysqli_query($con,$sql);
}
}
mysqli_close($con);
?>
If i remove the hash attempt and leave the pass word as it is received the password gets inserted so I believe it is the hashing function that is causing the 505. Can anyone see what is going wrong with my hash attempt?

insert into database not working

<?php
include("dbinit.php");
$text="helow";
$eadd = $_SESSION['account'];
$result = mysqli_query($link,"SELECT * FROM account where Eadd='". $eadd ."'");
while($row = mysqli_fetch_array($result))
{
$name = $row['Name'];
}
$ctext = $_POST['ctext'];
$sql = "INSERT INTO chat (By, Content, Reported) VALUES ('$name','$ctext','No')";
mysqli_query($link,$sql);
mysqli_close($link);
$text=$name . $ctext;
echo $text;
?>
Here is my code. In my other page, this works but .. when i change the values in "insert into" why i cant store it to database?
<?php
session_start();
$link = mysqli_connect("localhost", "xxx", "xxx", "xxx");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_select_db($link, "xxx");
header('Content-type: text/html; charset=utf-8');
?>
here is my dbinit file
$sql = "INSERT INTO `chat` (`By`, `Content`, `Reported`) VALUES ('".$name."','".$ctext."','No')";
to protect from mysql injections read this.
also, what kind of values did you change to that doesn't work?
BY is reserved keyword you should use backticks around it
$sql = "INSERT INTO chat (`By`, Content, Repo.........
Wrap the PHP variables in your $sql with double quotes, don't forget the dots.
$sql = "INSERT INTO chat (By, Content, Reported) VALUES ('".$name."','".$ctext."','No')";

Categories