INSERT INTO-Statement with PHP-Variable and SELECT-Statement [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I´m trying to insert a current SELECT-Statement into an Database table with an PHP-variable. It works by this way without the variable:
mysql_query("INSERT INTO table (column) SELECT column FROM table1");
Did anyone have a idea how I can fix it?
PHP: 5.3.10 and mysql-databse

try this
$sample="SELECT column FROM table1"; // your select query
$query="INSERT INTO `table` (column) $sample";
mysqli_query('database connection code',$query);
Let me know if any problem arises.

You can use mysql_insert_id() to get the last inserted ID value and try insert query separately.
<?PHP
$lastId = mysql_insert_id();
$query = mysql_query("Your Select Query with $lastId");
?>

Related

is it possible to load unique item or one of a kind item from sql using php? (check image) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
The image above is my sql database. what I want to do is load the names and echo in json but the thing is though, I want to show one of each name. Like see how there are 4 assassinshadow entries? I want php to echo only one of em. not making much sense am I? haha
You can use DISTINCT in your mysql query:
$mysqli = mysqli_connect("example.com", "user", "password", "database");
$query = "SELECT DISTINCT name from yourtable";
$res = mysqli_query($mysqli, $query);
$row = mysqli_fetch_assoc($res);
Two ways to do that:
SELECT distinct name FROM my_table
or
SELECT name FROM my_table group by name

Inserting php arrays into mysqli database rows? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In my MySQLi database, I have 2 columns in the table. One is 'author' and the other is 'books'. I have a variable called '$author' and an array called '$books' which contains unknown numbers of values.
I want to populate column 'books' with values inside array '$books' and in the other column variable '$author' which will remain constant in all rows.
Please help. It will be even more appreciated if you provide it in procedural way instead of OOP.
Assuming $author is a string and $books is an array of integers,
$sql = "INSERT INTO your_table (books,author) VALUES(";
foreach($books as $book_data){
$sql .= "($book_data,'$author'),";
}
$sql = rtrim($sql, ",") . ")";
//Execute the query
This will insert every $books variable as a new row with the constant $author.

How to print data from SQL database to HTML table based on query [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
so I have records in my database that I would like to print to a HTML table IF they belong to the user.
$sql="SELECT * FROM ".$tbl_name." WHERE Username='".$username."'";
The table would read :
Date, Room, Period , Cancel
With cancel being a red cross designed to delete that specific entry from the database :
Booking ID, Date, Period,Type,RoomID, Username are the fields in the database.
How would I go about doing this? I was thinking echoing a table using a for loop but I wouldn't know where to begin?
Try:
$query_rsSearch = "SELECT * FROM ".$tbl_name." WHERE Username='".$username."'";
$rsSearch = mysql_query($query_rsSearch) or die(mysql_error());
$row_rsSearch = mysql_fetch_assoc($rsSearch);
$totalRows_rsSearch = mysql_num_rows($rsSearch);
do {
echo $row_rsSearch['Date']." | ".$row_rsSearch['Room']." | ".$row_rsSearch['Period'];
} while($row_rsSearch = mysql_fetch_assoc($rsSearch));

Statement works in PgADMIN but not in PHP Script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This statement works in pgAdmin but not when run in a php script the php script can select all but can not update why is this?
UPDATE users SET password = '123123' WHERE email = 'random#random.com'
PHP code that doesn't work:
$sql = $dbh->prepare("UPDATE users SET password = '11111111111' WHERE email = 'test#outlook.com')");
$sql->execute(array());
PHP code that does work:
$sql = $dbh->prepare("SELECT * FROM users");
$sql->execute(array());
$fr = $sql->fetchAll(); var_dump($fr);
In your update query you've got ) at the end which will cause syntax error. Check it using eg. $dbh->errorInfo().
Also, don't use prepare() for queries that don't use parameters. Instead use query() for SELECT and exec() for others.

PHP to input email to database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
This is the site that I am trying to create the script for http://facechat.louisdickinson.com/
The idea is people can save there email to my database, and another button called "Start Call" will randomly select a email and call it using:
facetime://email#email.com
Effectively this will create a "omegle" style web-based facetime chat site.
I am new to MySQL and PHP and don't know where to start, any help will be appreciated!
I don't know, what kind of help do you need.. First of all, you should have a php script, witch can take the posted name/e-mail pair.
In this script, you should sanitize the posted values, than you can add it to your database with the following:
$query = "INSERT INTO <tableName> (`name`, `e-mail`) VALUES ( '".$postedName."', '".$postedMail."' )";
On button press, you should have another php script, for selecting the random e-mail:
$query = "SELECT COUNT(*) FROM <tableName>";
$max should be the query result.
$random = rand( 0 , $max - 1 );
$query = "SELECT `e-mail` FROM <tableName>" LIMIT $random, 1";
With this query you got a random e-mail.
Do you need more exact code? Please be more exact on what you need!
Kind regards,
hotzu
In php:
mysql_connect("host", "user", "password") or die(mysql_error());
mysql_selectdb("database"); // '' for an auto increment column
mysql_query("INSERT INTO TABLE VALUES ('$email', ''));
And for more info http://www.w3schools.com/php/php_mysql_intro.asp

Categories