mkebkr04 Posted November 21, 2008 Share Posted November 21, 2008 I am trying to figure out what the where clause would be to update a certian row in a column. It is a site where other people will be adding and editing entries to the database as well as delete. What I can't seem to understand is how to edit specific rows in general. Basically I am trying to figure out how you set up the php code to edit a row that isnt even there yet but will be down the road when someone actually fills out the form and it inserts data. Here is what I believe it should look like except for the where clause. Could it possibly need to be set to the primary key of the table? $sql = "UPDATE interns SET Business_name = $business where ?? = $?? Also is it possible to combine more than one SET clause under an update in the php code? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/133658-editing-the-database/ Share on other sites More sharing options...
Maq Posted November 21, 2008 Share Posted November 21, 2008 The best way is to try it and find out. Could it possibly need to be set to the primary key of the table? No. You can have the WHERE clause match anything that you want to update... Your syntax is more/less correct, please read this: Tizag - UPDATE Also is it possible to combine more than one SET clause under an update in the php code? Yes. Quote Link to comment https://forums.phpfreaks.com/topic/133658-editing-the-database/#findComment-695416 Share on other sites More sharing options...
gevans Posted November 21, 2008 Share Posted November 21, 2008 You should add a row called 'id' to your table, set is an an INT and auto-increment $sql = "UPDATE interns SET Business_name = '$business' where id = $id You'll pass the rows id to the query and use that. To combine more than one SET clause; $sql = "UPDATE interns SET Business_name = '$business', another_row='$foo', yet_another_row='$bar' where id = $id Quote Link to comment https://forums.phpfreaks.com/topic/133658-editing-the-database/#findComment-695417 Share on other sites More sharing options...
Maq Posted November 21, 2008 Share Posted November 21, 2008 $sql = "UPDATE interns SET Business_name = '$business', another_row='$foo', yet_another_row='$bar' where id = $id"; Forgot your end quote and semi Quote Link to comment https://forums.phpfreaks.com/topic/133658-editing-the-database/#findComment-695421 Share on other sites More sharing options...
gevans Posted November 21, 2008 Share Posted November 21, 2008 haha, didn't even look for it, copied his code... cheers Maq Quote Link to comment https://forums.phpfreaks.com/topic/133658-editing-the-database/#findComment-695426 Share on other sites More sharing options...
mkebkr04 Posted November 21, 2008 Author Share Posted November 21, 2008 Alright I guess I am still lost. So the id = $id is the primary key I am guessing? I did set up the database so it had and incremental column, which I set as primary key so is that what I am essentially doing is setting the where clause to the id of the specific entry the user clicked on? Also I need to change the same row of each column. Its not necessarily going to need to change every column but I need it to be that way just in case someone wants to enter a whole row. So will another_row take care of that or is it something else? If so is yet_another_row what I will use for everything past the first another_row? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/133658-editing-the-database/#findComment-695446 Share on other sites More sharing options...
gevans Posted November 21, 2008 Share Posted November 21, 2008 Yes, use your primary ke in place of id. Yes, sorry, I should've called them another_column and yet_another_column. And yes you can change those to the names of the columns you wish to update Quote Link to comment https://forums.phpfreaks.com/topic/133658-editing-the-database/#findComment-695450 Share on other sites More sharing options...
mkebkr04 Posted November 21, 2008 Author Share Posted November 21, 2008 Oh alright so if the primary key is intern_id then it will be where intern_id = $intern_id? Does the above essentially tell the php that intern_id in the database is the intern_id in the php code or so I have to set up a POST for intern_id? By adding columns is this what it will look like? SET Business_name = '$business', title = '$title', salary = '$salary',.....and so on... Thanks for the help so far Quote Link to comment https://forums.phpfreaks.com/topic/133658-editing-the-database/#findComment-695460 Share on other sites More sharing options...
gevans Posted November 21, 2008 Share Posted November 21, 2008 1. yes: WHILE intern_id='$intern_id' 2. yes, you need to POST intern_id using the row your user chose 3. The SET query looks fine Quote Link to comment https://forums.phpfreaks.com/topic/133658-editing-the-database/#findComment-695462 Share on other sites More sharing options...
mkebkr04 Posted November 29, 2008 Author Share Posted November 29, 2008 This is what I have so far. This is my edit.php. I also have a create.php that works good, which as you probably guessed creates the database using php. I was wondering how to go about setting up a view.php where there is a table with rows and colomns based on certian data from the database and thses rows and columns are set in stone. I am guessing a while or if loop maybe with the table tags in it. If this is correct does any one know of some good examples to follow when creating the view and then linking these together. if (isset($_POST['submit'])) { $business = $_POST['business']; $title = $_POST['title']; $salary = $_POST['salary']; $cname = $_POST['contact_name']; $cemail = $_POST['contact_email']; $cphone1 = $_POST['contact_phone1']; $cphone2 = $_POST['contact_phone2']; $business_url = $_POST['business_url']; $intern_url = $_POST['intern_url']; $fname = $_POST['faculty_name']; $femail = $_POST['faculty_email']; $fphone = $_POST['faculty_phone']; $sdate = sprintf('%d-%02d-%02d', $_POST['start_year'], $_POST['start_month'], $_POST['start_day']); $edate = sprintf('%d-%02d-%02d', $_POST['end_year'], $_POST['end_month'], $_POST['end_day']); $exdate = sprintf('%d-%02d-%02d', $_POST['exp_year'], $_POST['exp_month'], $_POST['exp_day']); $sdescription = $_POST['short_description']; $description = $_POST['description']; $con = mysql_connect("xxx", "xxx", "xxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("bakerjm", $con); $sql = "UPDATE interns SET Business_name = '$business', Job_title = '$title', Salary = '$salary', Business_contact_name = '$cname', Business_contact_email = '$cemail', Business_contact_phone1 = '$cphone1', Business_contact_phone2 = '$cphone2', Business_url = '$business_url', Internship_url = '$intern_url', Faculty_contact_name = '$fname', Faculty_contact_email = '$femail', Faculty_contact_phone = '$fphone', Internship_start_date = '$sdate', Internship_end_date = '$edate', Expiration_date = '$exdate', Short_description '$sdescription', Description = '$description' where Intern_id = $intern_id"; if (!mysql_query($sql, $con)) { die('Error: ' . mysql_error()); } echo "Submitted Successfully"; mysql_close($con); thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/133658-editing-the-database/#findComment-701484 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.