PHP / MySql - Concatenation in query - php

I am having problems concatenating a variable to a number (with the variable first, because $info1002 is not a known variable), I appreciate my problem here must be my single double quotations and I've tried a lot of combinations and my googling hasn't helped.
mysql_query("INSERT INTO users (ID, info1) VALUES ('','.$info.''002')")or die(mysql_error());

you need to format it like this:
mysql_query("INSERT INTO users (ID, info1) VALUES ('','".$info."002')") or die(mysql_error());
Also, if you have the ID field set to AutoIncrement, you can ommit it like this:
mysql_query("INSERT INTO users (info1) VALUES ('".$info."002')") or die(mysql_error());
This will insert value of $info followed by 002 into the database.

mysql_query("INSERT INTO users (ID, info1) VALUES ('','".$info."002"')")or die(mysql_error());

why don't you concatenate if before adding it to the query? I think it is much easier so you don't have that mass with quotation.
$var = $info.'002';
mysql_query("INSERT INTO users (ID, info1) VALUES (null ,'".$var."') or die(mysql_error());

mysql_query("INSERT INTO users (info1) VALUES ('{$info}002')")or die(mysql_error());
It may not work only if your ID is set NOT NULL and it not set as autoincrement!

Related

PHP - date(time) insert into DB

I'm using a time method:
$time= date('h:i:s');
What I want is to put this time into database in mySQL,
I used:
$query = mysqli_query($conn, "INSERT INTO tab ('ltime') VALUES ($time)"); but it's not working
Where tab is a table and a ltime is a Column with time Type.
What am I doing wrong?
Regards
Your query will goes like this.
$query = mysqli_query($conn, "INSERT INTO tab (ltime) VALUES ('$time')");
Your query should be like this
$query = mysqli_query($conn, "INSERT INTO tab (ltime) VALUES ($time)");
Columns and tables should have backticks and not single quotes
INSERT INTO `tab` (`ltime`) VALUES ($time)
$query = mysqli_query($conn, "INSERT INTO tab (ltime) VALUES ($time)");
Don't use '' when assigning the column name
The first way specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)VALUES (value1, value2, value3, ...);
If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query
INSERT INTO table_name VALUES (value1, value2, value3, ...);
In your case:
$query = mysqli_query($conn, "INSERT INTO tab (ltime) VALUES ($time)");
there is no need to single quote with column name:
Reference: https://www.w3schools.com/sql/sql_insert.asp

Can't get mysql_insert_id() method to grab value I need

I'm trying to grab SID from the insert into the first table (stories) so I can insert that SID into the writing table in my second insert.
I think the way to do this is with mysql_insert_id(); after the first query, but the primary key that auto-increments is called SID. If mysql_insert_id() could grab that value I'd be all set.
What I am finding from a var_dump is that the $SID = mysql_insert_id(); is just returning the value "0" and I'm not sure why.
There is a column called ID in stores, but if it was grabbing that, the value would be "1". Either way, I wish this method could be written as mysql_insert_SID();
Any idea what I am doing wrong or how I can fix this? And yes, I know this is a deprecated approach, but first I want to figure out how before I worry about converting to PDO.
// Get values from form
$category = $_POST['category'];
$genre = $_POST['genre'];
$story_name = $_POST['story_name'];
$text = $_POST['text'];
$query = "INSERT INTO stories (ID, category, genre, story_name, active) VALUES
('$user_ID', '$category', '$genre','$story_name', '1')";
$result = mysql_query($query);
$SID = mysql_insert_id();
$SID2 = "select stories.SID from stories where stories.SID=$SID";
$query2 = "INSERT INTO writing (ID, SID, text, position, approved)
VALUES('$user_ID', '$SID2', '$text', '1','N')";
$result = mysql_query($query2);
Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
(http://php.net/manual/en/function.mysql-insert-id.php)
But you aren't executing any query (via mysql_query()). You're just assigning your query to a variable. Try following:
$query = "INSERT INTO stories (ID, category, genre, story_name, active) VALUES
('$user_ID', '$category', '$genre','$story_name', '1')";
mysql_query($query);
$SID = mysql_insert_id();
I think you've forgotten to execute the query most probably?
Try
$SID = mysql_insert_id();
after executing the query
$query = "INSERT INTO stories (ID, category, genre, story_name, active) VALUES
('$user_ID', '$category', '$genre','$story_name', '1')";
$result = mysql_query($query); // executing
$SID = mysql_insert_id(); // order of queries is important
If you cannot get the value through mysql_insert_id() then try SELECT LAST_INSERT_ID(). Of course there will be a value if you have executed an insert query with AUTOINCREMENT (which you haven't done yet)

PHP - MySQL find row and replace

what im trying to figure out is how do i find something in my MySQL database and then replacing another row. Example
mysqli_query($con,"INSERT INTO persons (FirstName, LastName, Age)
VALUES ('$_POST[custom]', '$_POST[receiver_email]','$_POST[mc_gross]')");
$result = mysqli_query($con,"SELECT * FROM Persons
WHERE FirstName='bjarne'");
mysqli_query($con,"INSERT INTO persons (LastName)
VALUES ('$_POST[item_name]')");
Here i would like it to find where FirstName is "bjarne" and then replace his LastName with '$_POST[item_name]' in this case.
Try this:
$result = mysqli_query($con,"UPDATE `Persons` SET `LastName`='".$_POST['item_name']."'
WHERE `FirstName`='bjarne'");

PHP : how to insert (into an Sql table) a variable and concatenate it with the date function

i've been trying to insert a row into an Sql database table , and that row's last column is supposed to contain a variable and i can't figure out how to concatenate that variable with a date function. The problem becomes the single quote marks
$SQL = "INSERT INTO news VALUES (NULL, '$user', '$text'.'date('Y-m-d H:i:s')')";
That $text is supposed to have a "date now" function called right after it so that i would have the date that it was inserted into the table...
Thanks
Try this:
$sql = "INSERT INTO news VALUES (NULL, '$user', '$text<br>".date('Y-m-d H:i:s')."')";
You can try this
$SQL = "INSERT INTO news VALUES (NULL, '$user', '".$text." now()')";

PHP mySQL - Insert new record into table with auto-increment on primary key

Wondering if there is a shorthand version to insert a new record into a table that has the primary key enabled? (i.e. not having to include the key column in the query)
Lets say the key column is called ID, and the other columns are Fname, Lname, and Website
$query = "INSERT INTO myTable VALUES ('Fname', 'Lname', 'Website')";
Use the DEFAULT keyword:
$query = "INSERT INTO myTable VALUES (DEFAULT,'Fname', 'Lname', 'Website')";
Also, you can specify the columns, (which is better practice):
$query = "INSERT INTO myTable
(fname, lname, website)
VALUES
('fname', 'lname', 'website')";
Reference:
http://dev.mysql.com/doc/refman/5.6/en/data-type-defaults.html
I prefer this syntaxis:
$query = "INSERT INTO myTable SET fname='Fname',lname='Lname',website='Website'";
$query = "INSERT INTO myTable VALUES (NULL,'Fname', 'Lname', 'Website')";
Just leaving the value of the AI primary key NULL will assign an auto incremented value.
This is phpMyAdmin method.
$query = "INSERT INTO myTable
(mtb_i_idautoinc, mtb_s_string1, mtb_s_string2)
VALUES
(NULL, 'Jagodina', '35000')";
You can also use blank single quotes for the auto_increment column. something like this. It worked for me.
$query = "INSERT INTO myTable VALUES ('','Fname', 'Lname', 'Website')";

Categories