lolclol Posted May 5, 2013 Share Posted May 5, 2013 in the mr_recipes the field are id, username, name, ingredients, methods, URL and date. when someone using add recipe. the username is hidden, date and URL the URL is set to insert recipe.php?id=1 field "id 1" is a waiting for approval. I then have to go into my database to change the ?id=1 to the right ID number. what am wanting is to make it a little easier, so I need a page where . it shows all the url set to ?id=1 and where I can change it. for this I need to echo the id and have a edit form to change the url.. I can get it to echo this but noidea about the coding to change it. any help to direct me in the right direction Quote Link to comment Share on other sites More sharing options...
Barand Posted May 5, 2013 Share Posted May 5, 2013 Define the id column as auto_increment and do not include this column in the insert query. After the insertion you can call mysqli_insert_id to get the id of the newly inserted recipe Quote Link to comment Share on other sites More sharing options...
lolclol Posted May 5, 2013 Author Share Posted May 5, 2013 <?php $host="localhost"; // Host name $username="user"; // Mysql username $password="pass!"; // Mysql password $db_name="_login1"; // Database name $tbl_name="mr_recipes"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); // Count table rows $count=mysql_num_rows($result); ?> <table class="display" width="500" border="0" cellspacing="1" cellpadding="0"> <form name="form1" method="post" action=""> <tr> <td> <table width="500" border="0" cellspacing="1" cellpadding="0"> <tr> <td align="center"><strong>Id</strong></td> <td align="center"><strong>Name</strong></td> <td align="center"><strong>username</strong></td> <td align="center"><strong>cat</strong></td> <td align="center"><strong>url</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center"> <? $id[]=$rows['id']; ?><? echo $rows['id']; ?> </td> <td align="center"> <input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>"> </td> <td align="center"> <input name="username[]" type="text" id="username" value="<? echo $rows['username']; ?>"> </td> <td align="center"> <input name="cat[]" type="text" id="cat" value="<? echo $rows['cat']; ?>"> </td> <td align="center"> <input name="url[]" type="text" id="url" value="<? echo $rows['url']; ?>"> </td> </tr> <?php } ?> <tr> <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> </tr> </table> </td> </tr> </form> </table> <?php // Check if button name "Submit" is active, do this if($Submit){ for($i=0;$i<$count;$i++){ $sql1="UPDATE $tbl_name SET name='$name[$i]', username='$username[$i]', cat='$cat[$i]',url='$url[$i]' WHERE id='$id[$i]'"; $result1=mysql_query($sql1); } } if($result1){ header("location:editrecipe.php"); } mysql_close(); ?> find 2 codes that are simple like this one. just not doing anything. it shows fields from databases.. type in what I need to edit. then submit...but just refreshes the page with out updating the database Quote Link to comment Share on other sites More sharing options...
DavidAM Posted May 5, 2013 Share Posted May 5, 2013 Usually a column named "ID" holds a UNIQUE value that identifies ONE row (record) in the table. Generally it is an Auto-Increment value, so the server sets it on input and you never touch it. That script is assuming ID is a field of this type. There are other issues with that script, including the fact that the ID is not sent with the POST, so the updates are not going to work. Note that as it is, if ID is the PRIMARY KEY of your table, you can only ever have 1 pending recipe. When a second one is entered, it will overwrite the first, or the entry will fail. I would suggest you add a column to your table, perhaps: IsApproved BOOLEAN DEFAULT 0. Then update this column when you approve a recipe. You will have to make sure your scripts that list recipes do NOT include any that have a 0 in this column. Quote Link to comment 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.