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'");
Related
I am currently working with a form where you are able to create new users. You would specify the username, firstname, etc and a new record would be inserted into the Users table in a database.
Right after the creation of an user more steps follow, but for these having the auto generated UserID in the session would be very useful.
How can I accomplish this? I tried the following:
sqlsrv_query($conn1,"INSERT INTO Users (Username, Firstname, Lastname, JobTitle)
VALUES ('$_POST[Username]', '$_POST[Firstname]', '$_POST[Lastname]', '$_POST[JobTitle]'); SELECT SCOPE_IDENTITY() AS UserID;");
$result = sqlsrv_query($conn1); $next_result = sqlsrv_next_result($result); $row = sqlsrv_fetch_array($result);
$_SESSION['UserID'] = $result;
Use OUTPUT INSERTED in your query.
Change Your Code Like,
$result = sqlsrv_query($conn1,"INSERT INTO Users
(Username, Firstname, Lastname, JobTitle) OUTPUT INSERTED.UserID
VALUES ('$_POST[Username]', '$_POST[Firstname]', '$_POST[Lastname]', '$_POST[JobTitle]');
and you will get last inserted id in $result
I'm inserting data into members table, after I've inserted it I want to get the userID of the information just inserted. Whats the best way about doing this ?
My php code
//Insert info into database
$sql = "INSERT INTO members (type, firstname, lastname, email, password, bio) VALUES ('$usertype', '$firstname', '$lastname', '$emailsighup', '$passwordsighnup', '$bio')";
//Run a query to check if data has been inserted correctly.
$records = mysqli_query($connect, $sql);
I did try this SQL but was getting errors
$sql = "INSERT INTO members (type, firstname, lastname, email, password, bio) VALUES ('$usertype', '$firstname', '$lastname', '$emailsighup', '$passwordsighnup', '$bio')"; SELECT SCOPE_IDENTITY(userID);
Assuming $connect as the variable holding your connection information, which seems legit, you can get the value of the last id with:
$user_id = mysqli_insert_id($connect);
This is something you can do from the PHP MySQLi interface, rather than directly from SQL!
i want to insert a database table into another one which has more columns. I need all records of it. What i tried is following which is not working and does not give me an error message:
$sql = mysqli_query($con, "SELECT * FROM table1");
while ($row = mysqli_fetch_array($sql)) {
$sql1 = mysqli_query($con, "INSERT INTO table2
(uid,
pid,
tstamp,
crdate)
VALUES ('',
'".$row['value1']."',
'".$row['value2']."',
'".$row['value3']."'");}
you can directly do it like this, that's much faster and better than fetching the data and iterating over it.
insert into table2(uid,pid,tstamp,crdate)
select value1,value2,value3,value4 from table1
you forget close " :
mysqli_query($con, "INSERT INTO table2
(uid,
pid,
tstamp,
crdate)
VALUES ('',
'".$row['value1']."',
'".$row['value2']."',
'".$row['value3']."');");
also you can do this in query by select into to reduce consuming time:
INSERT INTO table2 (uid,pid,tstamp,crdate)
SELECT '',val1,val2,val3 FROM table1;
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!
ERD diagram
PHP code:
$insert_address = "insert into address (Street, Town, County, Postcode) values('$street', '$town', '$county', '$postcode')";
$run_address = mysqli_query($con, $insert_address);
$last_id = mysqli_insert_id($con);
$insert_user = "insert into user (UserIPAddress, Username, Email, Forename, Surname, Password, UserIcon, AddressID) values('$IP', '$username', '$email', '$firstname', '$secondname', '$password', '$userimage', '$last_id')";
$run_user = mysqli_query($con, $insert_user);
$sel_cart = "select * from cart where IPAddress='$IP'";
$run_cart = mysqli_query($con, $sel_cart);
$check_cart = mysqli_num_rows($run_cart);
In order for the AddressID to automatically populate my user table, I need to run the query before I run the insert query for my user table. However, inserting the 'UserID' into the address table requires the insert user query to have been run before the insert address query. I can't run two queries in one, so I deleted the UserID field from my Address table.
However, now I need the user to be able to update the details stored in the database tables. This works fine for the info in the user table, but not for the info in the address table. To combat this, I created a separate html page where the user could update their address, but without the userID I don't know how to get the right address for each user.
I'm up to
$get_address = "select * from address where AddressID='$address'";
But this obviously doesnt work.
Without the foreign key UserID I don't know how to make it work.
I've tried sessions but I really don't understand them.