PHP - Insert Into Table If Id Matches Session Id
Im trying to insert a name into my database but I need it to go into the column that matches the sessions user id
Code: [Select] "INSERT INTO users SET name = '".$_POST['name']."'"; how do I tell it to put name into name column with id matching Session id. really appreicate if someone could please help me with this. Similar TutorialsJust want to know if it is possible to do something like this? If so, how? Code: [Select] $qry = "SELECT * FROM users WHERE password='$password' AND username='$_SESSION['SESS_USER_NAME']'"; Came a long way with the code since Friday and have another issue.
I can echo the session username on my pages but not into the insert command to the database. I need this so when a user logs in only their data will be seen. Here is the code pages.
Here is the page code this does echo the username
Logged in as <?php echo "$username"; ?>
<?php // Start session session_start() ; $username = $_SESSION['username']; // Include required functions file require_once('include/db/functions.inc.php') ; // Check login status ... if not logged in, redirect to login screen if (check_login_status() == false) { redirect('login.php') ; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Add A New Tank(WAD)</title> <style type="text/css"></style> </head> <body> <p align="center"><a href="../test/index.php">Home</a> | <a href="../test/register.php">Register</a> | <a href="../test/login.php">Login</a> | <a href="../test/tank.php">Add Tank</a> | <a href="../test/fish.php">Add Fish</a> | <a href="../test/plants.php">Add Plants</a> | <a href="../test/water-test.php">Add Water Test</a> | <a href="../test/include/login/logout.inc.php">Logout</a></p></p> <p>Logged in as <?php echo "$username"; ?> </p> <table width="810" border="2" align="center"> <tr> <td> <table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td align="center" bgcolor="#FFFFFF" scope="col"><h2><b>Your Tanks(WAD)</b></h2></td> </tr> <form action="/test/include/tank/tank.inc.php" method="post" name="tank" id="tank"> <table border="2" align="center" cellpadding="0"> <tr> <td><div align="left"><b>Tank Name: </b> </div></td> <td><div align="left"> <input type="text" name="tankname" size="25" /> </div></td> </tr> <tr> <td><div align="left"><b>Date Filled With Water: </b> </div></td> <td><div align="left"> <input type="text" name="date" size="25" /> </div></td> </tr> <tr> <td><div align="left"><b>Length: </b> </div></td> <td><div align="left"> <input type="text" name="length" size="25" /> </div></td> </tr> <tr> <td><div align="left"><b>Depth: </b> </div></td> <td><div align="left"> <input type="text" name="depth" size="25" /> </div></td> </tr> <tr> <td><div align="left"><b>Height: </b> </div></td> <td><div align="left"> <input type="text" name="height" size="25" /> </div></td> </tr> <tr> <td><div align="left"><b>Volume: </b> </div></td> <td><div align="left"> <input type="text" name="volume" size="25" /> </div></td> </tr> <tr> <td><div align="left"><b>Type of Tank: </b> </div></td> <td><div align="left"> <input type="text" name="type" size="25" /> </div></td> </tr> <tr> <td></div></td></tr> <tr> <td><div align="left"><b>Notes: </b> </div></td> <td><div align="left"> <p> <textarea name="notes" cols="50" rows="10"></textarea> </p> </div></td> </tr> <tr> <th colspan="2"><p> <input type="submit" value="Add New Tank" /> </p></th> </tr> </table> </form> <tr> <td align="center" valign="top" bgcolor="#FFFFFF"><div align="center"><font size="2"> © 2014 <a href="http://www.pctechtime.com">PC TECH TIME</a> </font> </div></td> </tr> </table> <tr> <td></td></td></tr> <tr> <td></tr></td></tr> </table> </body> </html>This is tank.inc.php which works except for the username being inserted into the database. I did try removing the mysqli_close($con); but that didn't help. I did have it working when I removed the mysqli_close($con); but then I logged out and then back in and it stopped? <?php include_once "../../../test/include/db/db.inc.php"; // escape data and set variables $tankname = mysqli_real_escape_string($con, $_POST['tankname']); $date = mysqli_real_escape_string($con, $_POST['date']); $length = mysqli_real_escape_string($con, $_POST['length']); $depth = mysqli_real_escape_string($con, $_POST['depth']); $height = mysqli_real_escape_string($con, $_POST['height']); $volume = mysqli_real_escape_string($con, $_POST['volume']); $type = mysqli_real_escape_string($con, $_POST['type']); $notes = mysqli_real_escape_string($con, $_POST['notes']); $username = $_SESSION['username']; // # setup SQL statement $sql="INSERT INTO tank (tankname, username, date, length, depth, height, volume, type, notes) VALUES ('$tankname', '$username', '$date', '$length', '$depth', '$height', '$volume', '$type', '$notes')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } echo 'New Tank Added '; mysqli_close($con); ?> $result = mysqli_query($con,"SELECT * FROM $_SESSION['datakey']"; Hi
I am very new to PHP & Mysql.
I am trying to insert values into two tables at the same time. One table will insert a single row and the other table will insert multiple records based on user insertion.
Everything is working well, but in my second table, 1st Table ID simply insert one time and rest of the values are inserting from 2nd table itself.
Now I want to insert the first table's ID Field value (auto-incrementing) to a specific column in the second table (only all last inserted rows).
Ripon.
Below is my Code:
<?php $con = mysql_connect("localhost","root","aaa"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ccc", $con); $PI_No = $_POST['PI_No']; $PO_No = $_POST['PO_No']; $qry = "INSERT INTO wm_order_entry ( Order_No, PI_No, PO_No) VALUES( NULL, '$PI_No', '$PO_No')"; $result = @mysql_query($qry); $val1=$_POST['Size']; $val2=$_POST['Style']; $val3=$_POST['Colour']; $val4=$_POST['Season_Code']; $val5=$_POST['Dept']; $val6=$_POST['Sub_Item']; $val7=$_POST['Item_Desc']; $val8=$_POST['UPC']; $val9=$_POST['Qty']; $N = count($val1); for($i=0; $i < $N; $i++) { $profile_query = "INSERT INTO order_entry(Size, Style, Colour, Season_Code, Dept, Sub_Item, Item_Desc, UPC, Qty, Order_No ) VALUES( '$val1[$i]','$val2[$i]','$val3[$i]','$val4[$i]','$val5[$i]','$val6[$i]','$val7[$i]','$val8[$i]','$val9[$i]',LAST_INSERT_ID())"; $t_query=mysql_query($profile_query); } header("location: WMView.php"); mysql_close($con); ?>Output is attached. Hi I have two tables Employer and Job. Emp_ID as PK of Employer and Job_id as PK of Job. I want to put Emp_ID as Foreign key for jobs. and when the employer logs in his empid is being stored as a session and when he creates a job his empid will be entered in the job table. i have got the A table. and i want get from data this table to session. how can I ? Hello guys, this is my first post so sorry if I made any mistake
I need select record from one table and move to another table
But I get this message saying "Warning: mysqli_query() expects at least 2 parameters, 1 given in" I had that on line 158, but now i get on line 156
I start to do PHP and mysql few weeks ago, only respond i get from teacher is search and search.
<?php if (isset($_POST['username'])) { $searchq = $_POST['username']; mysqli_query("SELECT * FROM login WHERE username='$searchq'")or die ("could not search"); while($row = mysqli_fetch_array($con, $query)) { $username = $row['username']; $password = $row['password']; $age = $row['age']; $phonenumber = $row['phonenumber']; $nationality = $row['nationality']; mysqli_query("INSERT INTO admin SET username ='$username', password='$password', age='$age', phonenumber='$phonenumber', nationality='$nationality'" ) ; echo"Data successfully inserted"; } } ?>When i search i see this type of code "$data = mysqli_query" add variable before mysqli What should I do, to make it work. And send record from one table to another. Thank you hello, anybody able to see what im doing wrong? Code: [Select] $id2=mysql_insert_id(); $year=$_POST['y']; $yr=substr($year,-2); $mth=$_POST['m']; $init=$_POST['initial']; $jobnumber=$yr.$mth.'-'.$id2.$init; $query = "INSERT INTO jobs VALUES ( '', '$id2', '$_POST[initial]', '$_POST[y]-$_POST[m]-$_POST[d]',date '$_POST[contact]', '$_POST[contactphone]', '$_POST[customer]', '$_POST[address]', '$_POST[city]', '$_POST[postal]', '$_POST[province]', '$_POST[description]' )"; mysql_query($query) or die('Error, adding new job failed. Check you fields and try again.'); echo "You have successfully entered a new job. The job number is $jobnumber"; Hey guys, it seems that so far in my little script everything is working except for the MySQL insert Nothing seems to appear in the table it's supposed to insert too but i dont see any errors anywhere. Any help would be appreciated. I know it's very messy but it's my first actual 'project' Code: [Select] <?php $apiKey="<omitted>"; $playeritemsURL="http://api.steampowered.com/IEconItems_440/GetPlayerItems/v0001/"; $fullURL=$playeritemsURL."?key=".$apiKey."&SteamID=".$_GET["SteamID64"]."&format=xml"; $playerLocalName=$_GET["SteamID64"].".xml"; $ch=curl_init($fullURL); $fh=fopen($playerLocalName, 'w'); ?> You entered ID: <?php echo $_GET["SteamID64"];?></br> <?php echo $fullURL; curl_setopt($ch,CURLOPT_FILE,$fh); curl_exec($ch); curl_close($ch); ?> </br> <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("csv_db", $con); $readXML=file_get_contents($playerLocalName); $xml=simplexml_load_file($playerLocalName); $output=$_GET["SteamID64"].".defindex.txt"; $fh=fopen($output, 'w') or die("can't open file"); //echo $xml->items->item->defindex[0]; foreach ($xml->xpath('//defindex') as $defindex) { fwrite($fh, $defindex."\n"); echo $defindex."</br>"; mysql_query("INSERT INTO ".$_GET["SteamID64"]." (defindex) VALUES (".$defindex.")"); } ?> Hi there, I have a form which I want to submit data into my tables. There are going to be 4 tables involved with this form, and these 4 tables should relate to one another in some sort of way. My problem is either PHP or MySQL, but I keep getting a warning which I can't figure out. I remember this warning appearing even if the code before it is wrong, therefore I am not relying on it. This is what the error says: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in G:\xampp\htdocs\xampp\dsa\wp3.php on line 40 Here's my code: Code: [Select] <html> <head> <title>WP3</title> </head> <body> <form id="search" name="search" id="search" method="get" action="searchresults.php" /> <input type="text" name="terms" value="Search..." /> <input class="button" type="submit" name="Search" value="Search" /> </form> <?php include_once('connectvars.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if(isset($_POST['report'])){ $firstname = mysqli_real_escape_string($dbc, trim($_POST['firstName'])); $middlename = mysqli_real_escape_string($dbc, trim($_POST['middleName'])); $lastname = mysqli_real_escape_string($dbc, trim($_POST['lastName'])); $image = mysqli_real_escape_string($dbc, trim($_POST['image'])); $phone = mysqli_real_escape_string($dbc, trim($_POST['phone'])); $organisation = mysqli_real_escape_string($dbc, trim($_POST['organisation'])); $street = mysqli_real_escape_string($dbc, trim($_POST['street'])); $town = mysqli_real_escape_string($dbc, trim($_POST['town'])); $city = mysqli_real_escape_string($dbc, trim($_POST['city'])); if (!empty($firstname) && !empty($middlename) && !empty($lastname) && !empty($image) && !empty($phone) && !empty($organisation) && !empty($city)) { $query = "INSERT INTO report (organisation, phoneNo) VALUES ('$organisation', '$phone'); INSERT INTO person (firstName, middleName, lastName) VALUES ('$firstname', '$middlename', '$lastname'); INSERT INTO identification (image) VALUES ('$image'); INSERT INTO location (street, town, city) VALUES ('$street', '$town', '$city')"; $data = mysqli_query($dbc, $query); if (mysqli_num_rows($data) == 0) { mysqli_query($dbc, $query); echo "Thank you, your report has been received."; } else { // An account already exists for this username, so display an error message echo '<p>This report already exists.</p>'; $username = ""; } } else echo "Please enter all of the fields"; } ?> <form id="report_sighting" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <h2>Report a sighting</h2> <table> <tr> <td> <label>First name:</label> </td> <td> <input type="text" id="firstname" name="firstName" value="<?php if (!empty($firstname)) echo $firstname; ?>" /> </td> </tr> <tr> <td> <label>Middle name:</label> </td> <td> <input type="text" id="middlename" name="middleName" value="<?php if (!empty($middlename)) echo $middlename; ?>" /> </td> </tr> <tr> <td> <label>Last name:</label> </td> <td> <input type="text" id="lastname" name="lastName" value="<?php if (!empty($lastname)) echo $lastname; ?>" /> </td> <tr> <td> <label>Upload Identification:</label> </td> <td> <input type="file" id="image" name="image" /> </td> <tr> <tr> <td> <label>Contact phone number: </label> </td> <td> <input type="text" id="phone" name="phone" /> </td> <tr> <tr> <td> <label>Organisation: </label> </td> <td> <input type="text" id="organisation" name="organisation" /> </td> </tr> <tr> <td> <label>Street seen: </label> </td> <td> <input type="text" id="street" name="street" /> </td> </tr> <tr> <td> <label>Town seen: </label> </td> <td> <input type="text" id="town" name="town" /> </td> </tr> <tr> <td> <label>City seen: </label> </td> <td> <input type="text" id="city" name="city" /> </td> </tr> <tr> <td> </td> <td> <input type="submit" value="Report" name="report" /> </td> </tr> </table> </form> </body> </html> I've checked out the SQL statement and it's alright, so that leaves me with the PHP. I would very much appreciate if anyone could help me out here, thanks. Im trying to insert some values automatically into a table once the form loads, but Im getting an error. Here is the code Code: [Select] <?php $aid = $_GET['aid']; $sd = $_GET['sd']; ?> <style> #message {margin:20px; padding:20px; display:block; background:#cccccc; color:#cc0000;} </style> <div id="message">Your notification has been submitted.</div> <div style="text-align:center "> <?php $connection = mysql_connect("localhost", "username", "password"); mysql_select_db("articles", $connection); $query="INSERT INTO broken_links (articleid, article) VALUES ('$aid', '$sp')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Submitted"; mysql_close($con) ?> <table border="0" cellpadding="3" cellspacing="3" style="margin:0 auto;" > <input type="submit" id="Login" value=" Thank you. Please press to close " onclick="tb_remove()"></td> </tr> </table> </div> Any help will be appreciated Hi,
The following code was written by someone else. It allows me to upload images to a directory while saving image name in the mysql table.
I also want the code to allow me save other data (surname, first name) along with the image name into the table, but my try is not working, only the images get uploaded.
What am I missing here?
if(isset($_POST['upload'])) { $path=$path.$_FILES['file_upload']['name']; if(move_uploaded_file($_FILES['file_upload']['tmp_name'],$path)) { echo " ".basename($_FILES['file_upload']['name'])." has been uploaded<br/>"; echo '<img src="gallery/'.$_FILES['file_upload']['name'].'" width="48" height="48"/>'; $img=$_FILES['file_upload']['name']; $query="insert into imgtables (fname,imgurl,date) values('$fname',STR_TO_DATE('$dateofbirth','%d-%m-%y'),'$img',now())"; if($sp->query($query)){ echo "<br/>Inserted to DB also"; }else{ echo "Error <br/>".$sp->error; } } else { echo "There is an error,please retry or check path"; } } ?>joseph I am trying to insert records from an foreach array, it only inserts the first record and I cannot work out how to get it to loop through all of the records. Thank you foreach($_SESSION['post_data_array'] AS $seat) { $rowId = substr($seat, 0, 1); $columnId = substr($seat, 1); echo $rowId . $columnId . ", "; } $sql125 = "INSERT INTO booked_seats(booking_id, row_id, column_id) values ('$book_id', '$rowId', '$columnId')"; $result125 = mysql_query($sql125); if ($result125) { echo "worked"; } else { echo "didnt work"; } Hi, I'm quite new to php and not the greatest of coders, so urgently require your help. I want to be able to take data from an un-normalised table and load into 2 normalised tables as below: 1) Table 1 - All Music Tracks (Un-normalised table) Album Name Artist Name Track Name Track Length etc. 2) Table 2 - Albums Table (Normalised table) ID Artist Name Album Name etc. 3) Table 3 - Tracks Table (Normalised table) ID Track Name Album ID (foreign key to Albums table) etc. Can someone please provide some sample code as a starting point? The All Music Tracks table includes both the album name and track name in each row, but I only want the album appearing once in the Albums tables, would I have to do an Distinct select first to get the album names into an array? I'm not sure how I would then have nested loop to do the insert into the Tracks table. Much Appreciated, PD Hello, I'm working on a PHP register form, all I want to do, is to be able to insert an extra variable into the database: Code: [Select] // now we insert it into the database $insert = " INSERT INTO users (username, password) VALUES ('".$_POST['username']."', '".$_POST['pass']."') "; $add_member = mysql_query($insert); right, so I have the username and password variable being passed. But I also want to pass a variable into a column called 'weaponAttachments', all I want for this variable to be is 5000. So, in other words, something like this: 5000['weaponAttachments'] Thanks Hi, I would like to do the following in PHP using a MySQL database, but not sure how. select a, b,c etc.. from mailbox where id = X then insert id,a,b,c etc.. into problem When I select, I will be calling the id So I would like it to get data from mailbox and then insert it into problem. How would I go about doing this? Thanks for any help you can give me Code: [Select] $myusername = $_POST['myusername']; mysql_query("INSERT INTO logedin ( username ) VALUES ('$myusername')") OR die("Could not send the message: <br>".mysql_error()); wont insert anything no error's given even just trying to insert username into the the loged in table yes i know i spelt loged in wrong but i did that on the db lol I am under developing PHP add to cart without DB, so that i am using SESSION, actually get data from fetch_data.php data to my_cart.php using the POST method, successfully retun the values, After receiving the post data how can i convert to display like a table. workout: fetch_data.php return values. https://snag.gy/IASCMZ.jpg & values received https://snag.gy/ojWxHe.jpg Here is my my_cart.php : // FYI -> Here i am using only two fields : voice_sku & voice_name <table width="100%" cellpadding="6" cellspacing="0"> <thead> <tr> <th>Voice Sku</th> <th>Voice Name</th> <th>Remove</th> </tr> </thead> <tbody> <?php session_start(); $voice_sku = ''; $voice_name = ''; if(isset($_POST['voice_sku'])&& isset($_POST['voice_name'])) { // print_r($_POST); // die(); $voice_sku = $_POST['voice_sku']; $voice_name = $_POST['voice_name']; $_SESSION['voice_sku'] = $voice_sku; $_SESSION['voice_name'] = $voice_name; } var_dump($_SESSION); ?> <tr> <td colspan="5"> <span style="float:right;text-align: right;"> <!-- --> </span> </td> </tr> <tr> <td colspan="5"> <a href="index.php" class="button">Add More Items</a> <button type="submit">Update</button> </td> </tr> </tbody> </table>
Hi guys, Im making an event booking/reservation site, I have 5 tables (Malls, Events, Booking, Reservation and Users) which are all connected and doing fine. I created a function which is this... function select_all_events_by_mall_id($mall_id) { echo "<table id='event_table'>"; echo "<th>Event Name</th>"; echo "<th>Event Location</th>"; echo "<th>Number of Cars</th>"; echo "<th>Description</th>"; echo "<th>Booked</th>"; echo "<th>Reserved</th>"; echo "<th>Reserved Until</th>"; echo "<th>Vacant</th>"; $events_set = select_all_events($mall_id); while($events = mysql_fetch_array($events_set)) { echo "<tr>"; echo "<td>" . $events['event_name'] . "</td>"; echo "<td>" . $events['event_location'] . "</td>"; echo "<td>" . $events['number_of_cars'] . "</td>"; echo "<td>" . $events['description'] . "</td>"; // Booked echo "<td>"; $booked_set = select_all_booking($events['id']); while($booked = mysql_fetch_array($booked_set)) { $user_set = select_all_user_booked($booked['users_id']); while($user = mysql_fetch_array($user_set)) { echo $user['company'] . ", " . $user['branch'] . "<br />"; } } echo "</td>"; // Reserved echo "<td>"; echo "<ol>"; $reserved_set = select_all_reservation($events['id']); while($reserved = mysql_fetch_array($reserved_set)) { if(empty($reserved['events_id'])) { echo "No Reservation's "; } else { $reserved_user = select_all_user_reserved($reserved['users_id']); while($user_set = mysql_fetch_array($reserved_user)) { echo "<li>" . $user_set['brand'] . ", " . $user_set['branch'] . "</li>"; } } } echo "</ol>"; echo "</td>"; // Expiration Date echo "<td>"; echo "<ol>"; $reserved_set = select_all_reservation($events['id']); while($reserved = mysql_fetch_array($reserved_set)) { $reserved_user = select_all_user_reserved($reserved['users_id']); while($user_set = mysql_fetch_array($reserved_user)) { $exp_date = select_all_expiration_date($user_set['id']); while($date = mysql_fetch_array($exp_date)) { echo "<li>" . $date['expiration_date'] . "</li>"; } } } echo "</ol>"; echo "</td>"; //Vacant echo "<td>"; $vacant_set = select_all_booking($events['id']); $booking_count = mysql_num_rows($vacant_set); $reserved_count = mysql_num_rows($reserved_set); $total_count = $booking_count + $reserved_count; $vacant = $events['number_of_cars'] - $total_count; if($vacant < 0) { echo "This Event is fully booked"; } else { echo $vacant; } echo"</td>"; echo "</tr>"; } echo "</table>"; } The function starts by receiving an id from picking a mall name from a select tag. It works fine, it outputs the data on the page like i want it to, but my problem is that i need a way to add a submit or any button to each generated row that will, when clicked will insert the event id and user id (who clicked it) to my booking/reservation table as a new row. I tried adding a form inside my function and saving the event id every loop but when i click the submit button i think it doesn't work (or i made a mistake).. Sorry if my coding sucks, I'm kinda new in php. Sorry if its kinda confusing my head is spinning right now from thinking this stuff the whole day.. Thanks in advance for any comments and ideas. |