How can I put multiple php commands like:
$email = $_POST['email'];
//get the user.id
$result = mysql_query("SELECT user.id FROM user WHERE user.email LIKE '$email'");
$user_id = mysql_fetch_array($result);
$user_id = $user_id["id"];
//fill the album -> "Profilbilder"
mysql_query("INSERT INTO `album` (`user_id`, `name`) VALUES ('$user_id', 'Profilbilder')") or die(mysql_error());
//get the album.id
$result = mysql_query("SELECT album.id FROM album WHERE album.user_id = '$user_id'");
$album_id = mysql_fetch_array($result);
$album_id = $album_id["id"];
mysql_query("INSERT INTO `foto` (`album_id`, `name`, `zeitstempel`, `link`) VALUES ('$album_id', '$file_name', NOW(), '$file_path')") or die(mysql_error());
.. to get database information in one big command?
You can actually insert into a table the results from a query like so:
$query="INSERT INTO `album`
(`user_id`, `name`)
SELECT user.id, 'Profilbilder'
FROM `user`
WHERE user.email LIKE '%$email%'";
Related
I have two tables TableA and TableB with colums id, login, pass. Also I have a php array: $array = [1,3,5]. How can I transfer rows from TableA to TableB where id equal each value of my array? id is autoincremented and must be unique.
In my head it looks like this :
INSERT INTO TableB (`login`, `pass`)
SELECT `login`, `pass` FROM TableA WHERE `id` = $array[0]
AND `id` = $array[1] AND `id` = $array[2];
But it does not work
Is there any chances to do it in cycle using WHILE?
You can use a statement such as this:
$ids = [1,3,5];
$query = "INSERT INTO `TableB` (`login`, `pass`) SELECT `login`,`pass` FROM `TableA` WHERE `id` IN (".implode(",", $ids).");";
Although: You should be using prepared statements and parameters for this, this answer is just meant to show the syntax.
it is easy let's say you are using mysqli:
$query= "SELECT * FROM TableA limit 3";
//takes first 3 rows from TableA
//$con being the connection string in config file
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
//takes each row by row
$username = $row['username'];
$password = $row['password'];
//now that we initiated variables the sql query
$query1 = "INSERT INTO TableB (username, password) VALUES ('$username', '$password')";
mysqli_query($con,$query1);
}
Hope this helps
EDIT:
I just read that it should have an array so you should change your query to this:
for($i=0; $i<count(array), $i++){
$query= "SELECT * FROM TableA WHERE ID = '$array[i]'";
//$con being the connection string in config file
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result){
//takes each row by row
$username = $row['username'];
$password = $row['password'];
//now that we initiated variables the sql query
$query1 = "INSERT INTO TableB (username, password) VALUES ('$username', '$password')";
mysqli_query($con,$query1);
}
}
Right now, this is what I have:
$query = "INSERT INTO COMMENTS VALUES ('$user', '$comment', '$star')";
mssql_query($query, $connection);
$commentIDQuery = "SELECT SCOPE_IDENTITY() AS ins_id";
$CI = mssql_query ($commentIDQuery, $connection);
$commentID = mssql_fetch_row($CI);
$idQuery = "SELECT recipeid FROM t_recipe WHERE recipename = '$recipeName'";
$RID = mssql_query($idQuery, $connection);
$recipeID = mssql_fetch_row($RID);
$rcQuery = "INSERT INTO COMMENT_RECIPE VALUES ('$commentID[0]', '$recipeID[0]')";
mssql_query($rcQuery, $connection);
So how would I get that ins_id?
It adds it to the first table, which is comments, but not the relation table.
Using sql server 2008
What about this......
$query = "DECLARE #NewID INT
INSERT INTO COMMENTS VALUES ('$user', '$comment', '$star');
SELECT #NewID = SCOPE_IDENTITY();
INSERT INTO COMMENTS_RECIPE VALUES (#NewID, '$recipeid')";
$stmt = sqlsrv_query($conn,$query);
i'm sending a post request to this code:
$name = (string)$_POST['name'];
$id = (int)$_POST['id'];
$query = "INSERT INTO Users (name, userID) VALUES ('$name', '$id')";
$result = mysqli_query($link,$query);
Which works fine and it adds a row to the table. How do i check wether the userID all ready exist in one of the following rows?
Do it like this
$query = "SELECT COUNT(*) FROM Users WHERE userID = '$id'";
$result = mysqli_query($link,$query);
if ( mysqli_fetch_assoc($result) ) {
$message = "Already exists";
} else {
$query = "INSERT INTO Users (name, userID) VALUES ('$name', '$id')";
$result = mysqli_query($link,$query);
}
Try this
$name = (string)$_POST['name'];
$id = (int)$_POST['id'];
$res = mysqli_query($link, "SELECT * FROM Users WHERE userID = '$id' LIMIT 1 ");
if($row = mysqli_fetch_assoc($res))
{
echo "this user id is already exists";
}
else
{
$query = "INSERT INTO Users (name, userID) VALUES ('$name', '$id')";
$result = mysqli_query($link,$query);
echo "record inserted successfully ";
}
REMEMBER : always use LIMIT 1 when you trying to get exactly one result.
IF you have properly set 'id' as primary key or unique key in your table, you can use the modifier IGNORE in your query you don't get an error when you try to isert a duplicate.
Doing this will result in the row only being inserted if the value of the primary key wasn't already in the table.
$query = "INSERT IGNORE INTO Users (name, userID) VALUES ('$name', '$id')";
IF you haven't set a primary key in your table you will have to do a SELECT query to find out if a row with that id is already in your table.
Make the UserID an Unique Key.
If it already exists, your code will throw an error and the row will not be insterted.
alter table Users
add unique index Unique_user (userID (8000))
Before inserting the values to the table check whether the following user id exists in the table or not
You can do it in this way
$name = $_POST['name'];
$id = $_POST['id'];
$sql = "SELECT * FROM Users WHERE userID = '$id'";
$res= mysqli_query($sql);
$num = mysqli_num_rows($res);
if($num == 0)
{
$query2 = "INSERT INTO Users (name, userID) VALUES ('$name', '$id')";
$result2 = mysqli_query($query2);
echo "record inserted successfully ";
}
else
{
echo "Record Failed !!";
}
I want to get user name from a database table from his id
and put it in other data table
function upload_image($image_temp, $image_ext, $album_id, $image_n, $image_description) {
$album_id = (int)$album_id;
$image_n = mysql_real_escape_string(htmlentities($image_n));
$image_description = mysql_real_escape_string(htmlentities($image_description));
//$download_link = 'uploads/'. $album_id. '/'. $image['id']. '.'. $image_ext;
$mysql_date_now = date("Y-m-d (H:i:s)");
$user_name = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);
mysql_query("INSERT INTO `images` VALUES ('', '".$_SESSION['user_id']."','$user_name', '$album_id', UNIX_TIMESTAMP(), '$image_ext', '$image_n', '$image_description', '','$mysql_date_now')");
$image_id = mysql_insert_id();
$download_link = 'uploads/'. $album_id. '/'. $image_id. '.'. $image_ext;
mysql_query("UPDATE `images` SET `download_link`='$download_link' WHERE `image_id`=$image_id ");
$selection = mysql_query("SELECT `user_two` FROM `follow` WHERE `user_one`='".$_SESSION['user_id']."'");
while ($row = mysql_fetch_array($selection)) {
mysql_query("INSERT INTO `notification` VALUES ('', '".$_SESSION['user_id']."', '".$row['user_two']."', '', UNIX_TIMESTAMP(), '$image_n', '$image_description', '$download_link')");
}
$image_file = $image_id.'.'.$image_ext;
move_uploaded_file($image_temp, 'uploads/'.$album_id.'/'.$image_file);
Thumbnail('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/');
}
the problem is here
$user_name = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);
mysql_query("INSERT INTO `images` VALUES ('', '".$_SESSION['user_id']."','$user_name', '$album_id', UNIX_TIMESTAMP(), '$image_ext', '$image_n', '$image_description', '','$mysql_date_now')");
i get this in database (Resource id #14)
Your query doesn't return data: it returns a resource. You then have to work with the resource to retrieve your data, so in this line:
$user_name = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);
$user_name doesn't contain the information you want.
Try:
$result = mysql_query("SELECT `name` FROM `users` WHERE `user_id` = ".$_SESSION['user_id']);
list($user_name) = mysql_fetch_array($result);
Note: mysql is deprecated - use mysqli or PDO. The principle is the same.
I think you should look here:
MySQL syntax for Join Update
Or here:
MySQL Insert & Joins
You could do this in just one query with some joining of the tables.
Facebook and Twitter membership script error:
(does not record data to the database)
Login=>FB/TW DATABASE=>CONNECT OK!=>BACK TO MY WEBSITE=>MY DATABASE=>ERROR
SQL:
CREATE TABLE users
(
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(70),
oauth_uid VARCHAR(200),
oauth_provider VARCHAR(200),
username VARCHAR(100),
twitter_oauth_token VARCHAR(200),
twitter_oauth_token_secret VARCHAR(200)
);
loginFacebook.php:
<?php
require 'dbconfig.php';
class User {
function checkUser($uid, $oauth_provider, $username,$email,$twitter_otoken,$twitter_otoken_secret)
{
$query = mysql_query("SELECT * FROM `users` WHERE oauth_uid = '$uid' and oauth_provider = '$oauth_provider'") or die(mysql_error());
$result = mysql_fetch_array($query);
if (!empty($result)) {
# User is already present
} else {
#user not present. Insert a new Record
$query = mysql_query("INSERT INTO `users` (oauth_provider, oauth_uid, username,email,twitter_oauth_token,twitter_oauth_token_secret) VALUES ('$oauth_provider', $uid, '$username','$email')") or die(mysql_error());
$query = mysql_query("SELECT * FROM `users` WHERE oauth_uid = '$uid' and oauth_provider = '$oauth_provider'");
$result = mysql_fetch_array($query);
return $result;
}
return $result;
}
} ?>
Screen: Error
Column count doesn't match value count at row 1
What's wrong?
best regards,
Reformatted your query to be on multiple lines for readability:
$query = mysql_query(
"INSERT INTO `users`
(oauth_provider, oauth_uid, username, email, twitter_oauth_token, twitter_oauth_token_secret)
VALUES
('$oauth_provider', $uid, '$username','$email')
"
) or die(mysql_error());
You are specifying 6 columns in your INSERT statement, but only supplying 4 actual values for these columns. Either remove the last 2 twitter_oauth_* columns from your query, or supply the values for these columns.
What you have is
INSERT INTO `users` (oauth_provider, oauth_uid, username, email, twitter_oauth_token, twitter_oauth_token_secret)
VALUES ('$oauth_provider', $uid, '$username','$email')")
As you see you are trying to insert 6 values, however in query you are providing data of only 4 items ('$oauth_provider', $uid, '$username','$email'), it is showing error.
Below are two ways use you can use depending on the requirement you have.
Option 1
INSERT INTO `users` (oauth_provider, oauth_uid, username, email, twitter_oauth_token, twitter_oauth_token_secret)
VALUES ('$oauth_provider', $uid, '$username','$email',NULL, NULL)")
OR Option 2
INSERT INTO `users` (oauth_provider, oauth_uid, username, email)
VALUES ('$oauth_provider', $uid, '$username','$email')")