I need to insert the searched results into a table
i already done with getting the names of any value but its hard for me to INSERT them in the table.
here is some code:
<?php
$con=mysqli_connect("localhost", "root", "", "library");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//var_dump($_POST['genre']);
$genres = $_POST['genre'];
foreach ($genres as $k=>$v){
$rslt="INSERT INTO 'history'(id,name)
VALUES('NULL', '".$k."')";
}
If your id column is auto-increment you should not put NULL between quotes.
(That would mean you're inserting the string 'NULL' wich is not NULL).
You can find here the doc about inserting rows with an auto_increment value.
Try like this :
"INSERT INTO 'history'(id,name) VALUES(NULL, '".$k."')";
or this also works :
"INSERT INTO 'history'(name) VALUES('".$k."')";
Related
Could someone let me know how to insert a set of values into a table across different databases .
Example : I have a table by name TABLE inside Database1 , Database2 ....
Is there a way where I can insert values into the TABLE present in both the databses.
I tried doing
Insert into Database1,Database2.Table(type)values('1');
But it did not work .
Thanks for your help
Unfortunately Table names, column names, etc. cannot be dynamic in mysql, you can try to create dynamic queries
$databases = list of dbs;
foreach ($databases as $db){
$query = "Insert into".$db.".tableName()....."
}
since you are using php you can use mysqli_select_db(connection,dbname); to select different database using it.
for Eg.
//create a connection
$conn = mysqli_connect("host","user","password","db_name");
//list of databases
$databases = "Your Array of database";
foreach ($databases as $database){
// Change database to $database
mysqli_select_db($conn,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect :" . mysqli_connect_error();
}
// code for $database eg. - your insert query...
}
I am trying to select values from one DB. And insert and update the result into another. This is cronjob that needs to run everyday to replicate some data from one DB into another. I know I am missing steps / correct syntax, but I hope someone can help me out.
<?php
$con_1=mysqli_connect("host","user","pw","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$con_2=mysqli_connect("host","user","pw","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con_1,"SELECT id, name FROM table GROUP BY 1,2");
$mysqli->query($con_2, "INSERT INTO `table2`(`id`, `name`) VALUES ('".$result[1]."', ".$result[2].")
ON DUPLICATE KEY UPDATE name = ".$result[2]."");
}
mysqli_close($con_1);
mysqli_close($con_2);
?>
mysqli_query returns a query object, using $result[1] doesn't make sense, you need to fetch the rows in a loop:
while($row = $result->fetch_assoc()) {
// insert result in second database
}
For other access methods check the documentation.
I am trying to post some table rows into a database. I have created a while loop and it keeps posting my first HTML table row to the database. The desired behaviour is that every HTML table row is posted to the DB.
<?PHP
$con=mysqli_connect("localhost","root","","freoplanner");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$count=1;
$shiftDate='dp'.$count;
$shiftTime='shiftTime'.$count;
$shiftAantal='shiftAantal'.$count;
while(isset($_POST[$shiftDate]) && !empty($_POST[$shiftDate])){
$sql="INSERT INTO shifts (datum, tijd, aantal)
VALUES
('$_POST[$shiftDate]','$_POST[$shiftTime]','$_POST[$shiftAantal]')";
$count++;
mysqli_query($con,$sql);
}
header("location:shiftstoevoegen.php");
?>
You need to reinitialise the following inside the loop:
$shiftDate='dp'.$count;
$shiftTime='shiftTime'.$count;
$shiftAantal='shiftAantal'.$count;
currently, they will always be using $count = 1
I am currently using the following function:
if(isset($_REQUEST["function"]) && ($_REQUEST["function"] == "setnm")){
$value = $_REQUEST["value"]; //field to edit
$con=mysqli_connect("localhost", "root", "", "hike_buddy");
//Check Connection
if(mysqli_connect_errno())
{
echo "failed to connect:".mysqli_connect_error();
}
mysqli_query($con, "INSERT INTO user_com (name) VALUES ('$value')");
mysqli_close($con);
}
How can I alter this code so it will change the value of two fields?
For instance I have a comment and a name column and I want to update them both (different values) with one function.
Never use un-escaped strings specified by the user in your database queries.
So, if you're using mysqli:
$value1 = $con->real_escape_string($_REQUEST['value_1']);
$value2 = $con->real_escape_string($_REQUEST['value_2']);
$query = "INSERT INTO my_table (column_1, column_2) VALUES ('$value1', '$value2')";
$con->query($query);
Are you trying to INSERT new data in the database or UPDATE existing data?
If you want to update data, you should use the UPDATE statement:
$query = "UPDATE my_table SET column_1 = '$value1', column_2 = '$value2' WHERE my_table_key = '$key'";
Also, you need to escape these variables like har-wradim suggested.
You can do the following:
if(isset($_REQUEST["function"]) && ($_REQUEST["function"] == "setnm")){
$value = $_REQUEST["value"]; //field to edit
$comment = $_REQUEST["comment"]; //This is your comment
$con=mysqli_connect("localhost", "root", "", "hike_buddy");
//Check Connection
if(mysqli_connect_errno())
{
echo "failed to connect:".mysqli_connect_error();
}
//Edit the query like so to update insert the comment along with the name.
mysqli_query($con, "INSERT INTO user_com (name, comment) VALUES ('$value', '$comment')");
mysqli_close($con);
}
In my PHP code, I save a record like this:-
mysqli_query($con, "INSERT INTO levels (levelName)
VALUES ('" . $_POST["levelName"][0] . "')");
And this works fine.
In the table 'levels', there is an auto-incrementing PK field called "ID". How would I go about returning/echoing the value of that field when the record is saved to the database?
Quoting from the following reference:
How to Get the Unique ID for the Last Inserted Row
If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function.
# http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html
-
Example
<?php
$con = mysql_connect("localhost", "peter", "abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("test_db",$con);
$sql = "INSERT INTO person VALUES ('Børge','Refsnes','Sandnes','17')";
$result = mysql_query($sql,$con);
echo "ID of last inserted record is: " . mysql_insert_id();
mysql_close($con);
?>
from: http://www.w3schools.com/php/func_mysql_insert_id.asp
use
mysql_insert_id()
function
if using mysqli use something like this
$mysqli->insert_id