June 19, 201214 yr Ok I have a form that updates points on a user based on their name, u pick them from a drop down menu and simply insert the new points and press update, the drop down only appears to show their name where as i want their name AND points to be displayed, now i managed to do this with the following code but it fails to update the database? <form action="tourneyleaguepost.php" method="post"> <select id="Name" name="Name"> <?php $mysqlserver="localhost"; $mysqlusername="root"; $mysqlpassword="pw"; $link=mysql_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error()); $dbname = 'TourneyLeague'; mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error()); $namequery="SELECT * FROM tourneytable ORDER BY `Name` ASC "; $nameresult=mysql_query($namequery) or die ("Query to get data from firsttable failed: ".mysql_error()); while ($row=mysql_fetch_array($nameresult)) { $Name=$row[Name]; $Points=$row[Points]; echo "<option> $Name $Points </option>"; } ?> </select> <input name="Points" type="text" value="Insert points" size="8" style="color:#666;" onfocus="this.value=''" /><input type="submit" value="Update" /> </form> tourneyleaguepost.php <link href="style.css" rel="stylesheet" type="text/css" /> <?php define('DB_NAME', 'TourneyLeague'); define('DB_USER', 'root'); define('DB_PASSWORD', 'pw'); define('DB_HOST', 'localhost'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db(DB_NAME, $link); if (!$db_selected) { die('Can\'t use ' . DB_NAME . ': ' . mysql_error()); } $value = $_POST['Name']; $value2 = $_POST['Points']; $query="SELECT points FROM tourneytable WHERE Name='$value'"; $executequery=mysql_query($query); $array=mysql_fetch_array($executequery); if ($array[0]<$value2) { $query2="UPDATE tourneytable SET Points='$value2' WHERE Name='$value'"; mysql_query($query2); } if (!mysql_query($query)) { die('Error: ' . mysql_error()); } mysql_close(); ?> Successfully updated - <a href="javascript:history.go(-1);">go back</a><br /><br /> <html> <head> <script type="text/javascript"> function goBack() { window.history.back() } </script> </head> <body> <input type="button" value="Back" onClick="goBack()" /> </body> </html> Edited June 19, 201214 yr by Willows
June 19, 201214 yr Possibly because you haven't shown any code where a query would be run that actually updates said database. You'll need to show us the code in tourneyleaguepost.php
June 19, 201214 yr Author Possibly because you haven't shown any code where a query would be run that actually updates said database. You'll need to show us the code in tourneyleaguepost.php Sorry im completely not with it today, i'll edit the original post now
June 19, 201214 yr I got three errors on your form. The first here: $link=mysql_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error()); localhost not enclosed in quotes. Same here: $Name=$row[Name]; $Points=$row[Points]; Your select is populated with echo "<option> $Name $Points </option>"; so $_POST['Name'] will contain a value like: 'John 1500' and your $query will be SELECT points FROM tourneytable WHERE Name='John 1500' and your update statement never executes.
June 20, 201214 yr Author I got three errors on your form. The first here: $link=mysql_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error()); localhost not enclosed in quotes. Same here: $Name=$row[Name]; $Points=$row[Points]; Your select is populated with echo "<option> $Name $Points </option>"; so $_POST['Name'] will contain a value like: 'John 1500' and your $query will be SELECT points FROM tourneytable WHERE Name='John 1500' and your update statement never executes. Sorry I couldnt really follow your post, so your suggestions are the errors in which causes it not to update? coz it worked fine prior to adding the points part so i just need help adjusting that? Do you get an error code ? nope no error code just doesnt update the db like it did prior to adding the points in the echo Edited June 20, 201214 yr by Willows
June 20, 201214 yr Anonimista is right you should remove the "$points" part out of echo "<option> $Name $Points </option>"; It should be like: echo "<option>".$Name."</option>"; Edited June 20, 201214 yr by Leonvv
June 20, 201214 yr Author Anonimista is right you should remove the "$points" part out of echo "<option> $Name $Points </option>"; It should be like: echo "<option>".$Name."</option>"; yeh but thats the only way i can ahve the select form show the name and points in the drop down box, it just doesnt update
June 20, 201214 yr Try changing this: $value = $_POST['Name']; In this $parts= explode(" ",$_POST['Name']); $namelength=strlen($parts[0])-1; $value=substr($parts[0],0,$namelength); This code breaks the value of option around the space, and than remove the space. So everything what will be left, is the name. Let it know if it dosen't work
June 20, 201214 yr Author Try changing this: $value = $_POST['Name']; In this $parts= explode(" ",$_POST['Name']); $namelength=strlen($parts[0])-1; $value=substr($parts[0],0,$namelength); This code breaks the value of option around the space, and than remove the space. So everything what will be left, is the name. Let it know if it dosen't work sorry i dont quite follow, u told me to change $value = $_POST['Name']; in $parts= explode(" ",$_POST['Name']); $namelength=strlen($parts[0])-1; $value=substr($parts[0],0,$namelength); but that doesnt exist in there? i apologize in advance if im acting like a complete noob it is because i am one, basically the database works fine updates perfectly and everything is great, alls i want to do is display the current points of members in the drop down menu when its selected rather than just the name..
June 20, 201214 yr sorry i dont quite follow, u told me to change $value = $_POST['Name']; in $parts= explode(" ",$_POST['Name']); $namelength=strlen($parts[0])-1; $value=substr($parts[0],0,$namelength); but that doesnt exist in there? i apologize in advance if im acting like a complete noob it is because i am one, basically the database works fine updates perfectly and everything is great, alls i want to do is display the current points of members in the drop down menu when its selected rather than just the name.. It works this way: The PHP gets the name out of the drop down menu and uses it in the query. It gets every row in the database where the name is the value of the drop down menu. But if you change the value of the dropdown options, wich you did by adding the points in it. The value of the <option> tag dosen't equal with the corresponding name in the database, because in the dropdown menu the points are written behind the name, and in the database they're not. So what we need to achieve is to only keep the name, and not the points. So remove this line off you code $value = $_POST['Name']; And instead off that, paste this: $parts= explode(" ",$_POST['Name']); $namelength=strlen($parts[0])-1; $value=substr($parts[0],0,$namelength); Sorry for my bad english, I tried my best, Let me know if you still have questions. Edited June 20, 201214 yr by Leonvv
June 20, 201214 yr Author It works this way: The PHP gets the name out of the drop down menu and uses it in the query. It gets every row in the database where the name is the value of the drop down menu. But if you change the value of the dropdown options, wich you did by adding the points in it. The value of the <option> tag dosen't equal with the corresponding name in the database, because in the dropdown menu the points are written behind the name, and in the database they're not. So what we need to achieve is to only keep the name, and not the points. So remove this line off you code $value = $_POST['Name']; And instead off that, paste this: $parts= explode(" ",$_POST['Name']); $namelength=strlen($parts[0])-1; $value=substr($parts[0],0,$namelength); Sorry for my bad english, I tried my best, Let me know if you still have questions. hmm, the points aren't even displaying now, i think im going to have to edit this thread at a later date and make what im trying to achieve more clear, thanks for your time anyways!
June 20, 201214 yr hmm, the points aren't even displaying now, i think im going to have to edit this thread at a later date and make what im trying to achieve more clear, thanks for your time anyways! You're welcome, you can also PM me if you want
June 20, 201214 yr Author You're welcome, you can also PM me if you want Thanks man Im gunna pm you in a sec, do you have MSN or SKYPE?
Create an account or sign in to comment