I am working on a food Restaurant Website and whenever I try to submit the data to the database it displays: Unknown database foodies. "foodies" is the name of my database. I have also gone inside the phpmyadmin to confirm that my database "foodies" is existing, I also confirmed that every columns are created correctly. But I don't know why it keeps on displaying Unknown database foodies. Please I need help. below is the code:
<?php
if(isset($_POST['submit']))
{
// if button is clicked
//1. This code will get the data from the form.
$full_name = $_POST['full_name'];
$username = $_POST['username'] ;
$password = md5($_POST['password'] ); //password encrypted with md5
// 2.the sql query will send the collected from the form to the database
$sql = " INSERT INTO admin_1 SET
full_name ='$full_name',
username = '$username',
password ='$password'
";
// 3.This will execute the query and save it into the database
$conn = mysqli_connect('localhost','root','<password>') or die(mysqli_error($conn));
$db_select = mysqli_select_db($conn,'foodies') or die(mysqli_error($conn));
$res = mysqli_query($conn, $sql) or die(mysqli_error($conn));
}
?>
My case now is every username can select the items in spinner one time only. Mean if the spinner has 5 item, the user can choose all of them but all of them just can one time only. Below are my select data php:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$username = $_POST['username'];
$name = $_POST['name'];
//Creating an sql query
$sql = "INSERT INTO Selection (username, name) VALUES
('$username','$name')";
//Importing our db connection script
require_once('dbConnect.php');
//Executing query to database
if(mysqli_query($con,$sql)){
echo 'Select Successfully';
}else{
echo 'Sorry, You Already Select it Before';
}
//Closing the database
mysqli_close($con);
}
The name in this php means the item in spinner. I am no idea how to set every username can select all the item in spinner one time only. I am using localhost phpmyadmin.
You can specify unique constraint for username and name columns.
Alter the Selection table using this code:
ALTER TABLE `Selection` ADD UNIQUE `unique_index`(`username`, `name`);
Now if you try to insert any username and name pair that is already inserted, will fail.
Why are you not testing if the entry is already on database before you do insert? May be this (Untested) code might help:
if($_SERVER['REQUEST_METHOD']=='POST')
{
//Getting values
$username = $_POST['username'];
$name = $_POST['name'];
//Importing our db connection script
require_once('dbConnect.php');
//Creating an sql query
$check_sql = "SELECT * FROM Selection WHERE username='$username' AND name='$name' LIMIT 1";
$check_res = $mysqli->query($con,$check_sql);
if($check_res && mysqli_num_rows($check_res) >0)
{
echo 'Sorry, You Already Select it Before';
}
else
{
$sql = "INSERT INTO Selection (username, name) VALUES ('$username','$name')";
if(mysqli_query($con,$sql))
{
echo 'Select Successfully';
}
else
{
echo "Select failed for some other reason";
}
}
//Closing the database
mysqli_close($con);
}
I think for checking the user to spin it only once only you need to add a flag in your database structure such as
u_id | flag |
--------------
1 | 1
--------------
So that when retrieving or fetching or you can say while checking you just have to make sure that this particular u_id has already spin it once so further it can't be allowed.
So before inserting check the username or user_id of particular.
$sql = "SELECT u_id from user_spins table where u_id = 1 AND flag = 1";
//if yes then don't allow to proceed
//if no then insert into User_spins table
$sql = "INSERT INTO User_spins (u_id, name,flag) VALUES
('$username','$name',1)";
//Importing our db connection script
require_once('dbConnect.php');
Let's start that I am newbie in php, so still I am trying to learn. I have created a form on Wordpress and I want to insert the values on one table (data_test table, i have managed that) and then take all the columns from data_test table(id that is auto increment number,name,email,product, quantity that the user enter) and insert to other table. I used this html code for the form to parse the values:
<form action="../enter_data_insert.php" method="post" onsubmit="return form_validation()" name="myForm">
Name <input id="name" name="name" type="text" />
Email <input id="email" name="email" required type="email"/>
Product<input id="prod" name="prod" required type="text" />
Quantity<input id="quant" name="quant" required type="number" min="1" / >
<input type="submit" value="Submit" />
</form>
And then this php to take the values:
<?php
if(!empty($_POST["name"]) && !empty($_POST["email"]) && !empty($_POST["prod"]) && !empty($_POST["quant"])){
//connect with database
include "database_conn.php";
//get the form elements and store them in variables
session_start();
$name=$_POST["name"];
$email=$_POST["email"];
$prod=$_POST["prod"];
$quant=$_POST["quant"];
//insert data on data_test table
$sql="INSERT INTO `site_db`.`data_test` ( `name` , `email`, `prod`,`quant`) VALUES ( '$name','$email','$prod','$quant')";
if(!mysqli_query($con,$sql)){
echo mysqli_error($con);
} else{
//retrieve data
$sql = "SELECT data_test_id FROM data_test WHERE prod='$prod'";
$result = mysqli_query($con,$sql);
if(!$result){
echo mysqli_error($con);
} else{
while($value = mysqli_fetch_object($result)){
$id = intval($value->id);
$_SESSION['myid'] = $value->id;
var_dump($value);
//insert data on data_test_ins table
$sql="INSERT INTO site_db.data_test_ins` ( id,name , email, prod,quant) VALUES ( $id,'$name','$email','$prod','$quant')";
if(!mysqli_query($con,$sql)){
echo mysqli_error($con);
} else{
//Redirects to the specified page
// header("Location: http://localhost/site/");
}
}
}
}
}
?>
Now it inserts all the values except the id on data_test table, i guess that it is null because it must close the first insert on php and then i have to call a second insert (with //insert data on data_test_ins table) on other php?
But i am not sure, can anyone help me please? or just guide me what is the right way to do.
I start to think that i have to create two php to parse the values and take on the first table and then on the other php to insert the values?
Any thoughts are helpful! :-)
What you are doing is not right. It is not a good approach to add value to id field to the database manually. It should be generated automatically by the database. What I would recommend is, add another field to your data_test_ins table eg: test_id which points to the id of your data_test table. This is the concept of foreign key.
Read about the concept of foreign keys here
Your code would now be:-
<?php
if(!empty($_POST["name"]) && !empty($_POST["email"]) && !empty($_POST["prod"]) && !empty($_POST["quant"])){
//connect with database
include "database_conn.php";
//get the form elements and store them in variables
session_start();
$name=$_POST["name"];
$email=$_POST["email"];
$prod=$_POST["prod"];
$quant=$_POST["quant"];
//insert data on data_test table
$sql="INSERT INTO `site_db`.`data_test` ( `name` , `email`, `prod`,`quant`) VALUES ( '$name','$email','$prod','$quant')";
if(!mysqli_query($con,$sql)){
echo mysqli_error($con);
} else{
//retrieve data
$sql = "SELECT data_test_id FROM data_test WHERE prod='$prod'";
$result = mysqli_query($con,$sql);
if(!$result){
echo mysqli_error($con);
} else{
while($value = mysqli_fetch_object($result)){
$id = $value->id;
//insert data on data_test_ins table
$sql="INSERT INTO `site_db`.`data_test_ins` ( `id`,`name` , `email`, `prod`,`quant`, `test_id`) VALUES ('$name','$email','$prod','$quant', '$id')";
if(!mysqli_query($con,$sql)){
echo mysqli_error($con);
} else{
//Redirects to the specified page
header("Location: http://localhost/site/");
}
}
}
}
}
?>
have you tried passing $value->id into the query instead of $value?
its an object which has the current row of a result set, so you should only pass the id attribute of this object.
$sql="INSERT INTO `site_db`.`data_test_ins` ( `id`,`name` , `email`, `prod`,`quant`) VALUES ( '$value->id','$name','$email','$prod','$quant')";
Addition:
stop using the mysql deprecated library.
you should check the posted data if its isset or not
EDIT:
your code should looks like:
<?php
if(!empty($_POST["name"]) && !empty($_POST["email"]) && !empty($_POST["prod"]) && !empty($_POST["quant"])){
//connect with database
include "database_conn.php";
//get the form elements and store them in variables
session_start();
$name=$_POST["name"];
$email=$_POST["email"];
$prod=$_POST["prod"];
$quant=$_POST["quant"];
//insert data on data_test table
$sql="INSERT INTO `site_db`.`data_test` ( `name` , `email`, `prod`,`quant`) VALUES ( '$name','$email','$prod','$quant')";
if(!mysqli_query($con,$sql)){
echo mysqli_error($con);
} else{
//retrieve data
$sql = "SELECT data_test_id FROM data_test WHERE prod='$prod'";
$result = mysqli_query($con,$sql);
if(!$result){
echo mysqli_error($con);
} else{
while($value = mysqli_fetch_object($result)){
$_SESSION['myid'] = $value->data_test_id;
$id = intval($value->data_test_id);
//insert data on data_test_ins table
$sql="INSERT INTO `site_db`.`data_test_ins` ( id,name , email, prod,quant) VALUES ( '$id','$name','$email','$prod','$quant')";
if(!mysqli_query($con,$sql)){
echo mysqli_error($con);
} else{
//Redirects to the specified page
header("Location: http://localhost/site/");
}
}
}
}
}
?>
I'm trying to convert a previous line I had where I was calling something back from the database, and insert it instead.
This is the function I have, but I can't get the INSERT INTO to work correctly
I've already debugged that: the connection to the DB is working fine, the session var for user is set and that the $avatarID is present.
if(!empty($_SESSION['user'])){
$avatarID = $_POST['avatarID'];
$avatarID = mysql_real_escape_string(trim($_POST['avatarID']));
// Insert into DB
$sql = "INSERT INTO `users` (`avatar`) VALUES ('{$avatarID}') WHERE `username` = '".$_SESSION['user']."'";
$query = mysql_query($sql);
if($query === false){
return false;
}else{
return true;
}
header('Location: profile.php');
}
I think it's an issue with the $sql line. I'm not getting any errors other than a simple blankpage/dead screen.
Attempted changing to just the following:
// Insert into DB
$query = mysql_query("INSERT INTO `user` (`avatar`) VALUES ('{$avatarID}'") or die(mysql_error());
Edit OK so I realise the mistake I have made, as this should be an UPDATE WHERE not INSERT INTO. But I am still struggling to get the query details correct even when using UPDATE and WHERE. But still no result:
<?php session_start();
require 'connect.php';
if(!empty($_SESSION['user'])){
$avatarID = $_POST['avatarID'];
$avatarID = mysql_real_escape_string(trim($_POST['avatarID']));
// Insert into DB
$sql = "UPDATE `users` SET `avatar`='{$avatarID}' WHERE `username` = '".$_SESSION['user']."'";
$query = mysql_query($sql);
if($query === false){
return false;
}else{
return true;
}
header('Location: profile.php');
}else{
header('Location: choose-avatar.php');
}
?>
Use UPDATE instead of INSERT
$sql = "UPDATE `users` SET `avatar`='{$avatarID}' WHERE `username` = '".$_SESSION['user']."'";
I have a simple script which I have included here. the select query works fine but the insert query fails. I am running php, apache and mysql on my macbook.
The table city_profile has ID as a auto increment primary key. And name is a non-null.
function testMySQL() {
$db = new mysqli('localhost', 'root', NULL, 'citee');
//$query = "select * from city_profile"; //this query works
$query = "insert into city_profile ('name','state','country') values ('charlotte','north carolina','usa')";
//whereas the above one fails..
$results = $db->query($query);
if($results) {
echo '<p>The query is successful.</p>';
}else {
echo '<p>The query is NOT successful.</p>';
}
//close the connection
$db->close();
}
try to change this line:
$query = "insert into city_profile ('name','state','country') values ('charlotte','north carolina','usa')";
into this:
$query = "insert into `city_profile` (`name`,`state`,`country`) values ('charlotte','north carolina','usa')";