Jump to content

MySQL Update miltiply form Inputs to a database


Tiltan

Recommended Posts

Hello!

Well i have been trying to find this for a while, but i just cant get it to work. So i was hoping for some help here.

 

Its working to 90% now...

It get the data from the database.

Puts the data in a table, and in a form were the fields are all filled, using the data from the database.

 

Now when i submit the form it dosent update everything. It only updates one row.

 

Iv been reading and searching, finding some stuff but i just cant get my code to work properly...

 

Here is the entire code without any changes.

Function EditUserLinks(){
	global $database, $session;

/*Only verify/validate form when it is submitted program name: form.php */
if(isset($_POST['subEditLink'])){

$id = mysql_real_escape_string($_POST['id']);
$username = mysql_real_escape_string($_POST['username']);	
$rank = mysql_real_escape_string($_POST['rank']);
$rank2 = mysql_real_escape_string($_POST['rank2']);
$url = mysql_real_escape_string($_POST['url']);
$target = mysql_real_escape_string($_POST['target']);
$namn = mysql_real_escape_string($_POST['namn']);

  $error='';//initialize $error to blank
  

  if(trim($rank)=='' ||strlen(trim($rank)) >5){
      $error.="Please enter a Rank between 1 and 5 Numbers!<br />"; //concatenate the $error Message with a line break
  	}
  else {
        if(!eregi("^[0-9]+$", $rank)) {
        $error.="The rank you entered was not in the proper format!<br />";
        
        }
    }

  if(trim($rank2)=='' ||strlen(trim($rank2)) >5){
      $error.="Please enter a Rank 2 between 1 and 5 Numbers!<br />"; //concatenate the $error Message with a line break
  	}
  else {
        if(!eregi("^[0-9]+$", $rank2)) {
        $error.="The rank 2 you entered was not in the proper format!<br />";
        
        }
    }

  if(trim($url)=='' || strlen(trim($url)) > 200){
      $error.="Please enter a URL!<br />"; //concatenate the $error Message with a line break
  }
  	 /*else {
	if(!eregi("(ht|f)tp(s?)\:\/\/[a-zA-Z0-9\-\._]+(\.[a-zA-Z0-9\-\._]+){2,}(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+=&%\$#_]*)? ", $url)){
		$error.="The URL you entered was not in the proper format!<br />";
	} 
}*/
  if(trim($target)==''){
    $error.="A Target is required!<br />";
  }
   		elseif(!trim($target)=='_blank' || !trim($target)=='_self'){
		$error.="Hmm!<br />";
}

  if(trim($namn)=='' ||strlen(trim($namn)) >50){
      $error.="Please enter a Link Name between 1 and 50 characters!<br />"; //concatenate the $error Message with a line break
  }
  
  if($error==''){
      
  $q = "UPDATE links SET rank='".$rank."', rank2='".$rank2."', url='".$url."', target='".$target."', namn='".$namn."' 
  		WHERE id='".$id."' AND username='".$session->username."'";
  		

      	if (!mysql_query($q))
  			{
  			die('Error: ' . mysql_error());
  			}
		echo "1 record added";
      
  }
    else{
         echo "<span style=color:red>$error</span>";
      //  foreach ($err as $key => $error){
      //  $rank = $error;
//	}
        
    }                
}
		// User is logged in 
		$q = "SELECT * FROM links WHERE username='$session->username' ORDER BY rank DESC";
	$result = $database->query($q);
	// Error occurred, return given name by default 
	$num_rows = mysql_numrows($result);
   		if(!$result || ($num_rows < 0)){
      		echo "Error displaying info";
      		return;
   		}
   		if($num_rows == 0){
     		 echo "You dont have any Links ";
      		return;
   		}
   		echo'
   		<form id="form" method="post" action="useredit.php?option=editlinks">
   		<input type="hidden" name="subEditLink" value="subEditLink">
	<table ALIGN="left" BORDER="0" CELLSPACING="5" CELLPADDING="" id="table">
  			<tr>
    		 <td>Rank</td>
    		 <td>Rank2</td>
    		 <td>URL</td>
    		 <td>Target</td>
    		 <td>Namn</td>
  			</tr>';
  			
   		for($i=0; $i<$num_rows; $i++){
   			$id = mysql_result($result,$i,"id");
   			$username = mysql_result($result,$i,"username");
      		$rank = mysql_result($result,$i,"rank");
      		$rank2 = mysql_result($result,$i,"rank2");
      		$url = mysql_result($result,$i,"url");
      		$target = mysql_result($result,$i,"target");
      		$namn = mysql_result($result,$i,"namn");
    
	echo   '<tr>
				<input type="hidden" name="id" id="hiddenField" value="'.$id.'"/>
				<input type="hidden" name="username" id="hiddenField" value="'.$username.'"/>
    				<td><input type="text" name="rank" id="linkform" value="'.$rank.'" maxlength="5" size="5" /></td>
    				<td><input type="text" name="rank2" id="linkform" value="'.$rank2.'" maxlength="5" size="5" /></td>
    				<td><input type="text" name="url" id="linkform" value="'.$url.'" maxlength="200" size="55"/></td>
    				<td><select name="target" id="linkform" size="4">
    						<option selected="selected">'.$target.'</option>
    						<option>_blank</option>
    						<option>_self</option>
  				</select></td>
    				<td><input type="text" name="namn" id="linkform" value="'.$namn.'" maxlength="100" size="25"/></td>
  				</tr>';
	}
	echo '<tr>
			<td>
				<input type="submit" name="button" id="subform" value="Edit" />
			</td>
		  </tr>
		</table>
	</form>';
} // END EditUserLinks()

 

Would be nice to get any comments on if the code is safe and any changes i should make :D

 

Allso, the part on validating the "url" any help there? The goal is to validate that it is a proper url basicly, but very flexible, since the link can go anywere.

Perhaps its better to just make sure its a safe entry and not check it further, its for the user himself anyway.

Allso i read somewere(this forum i guess :) ) that leaving the "http://" part out is better and just add it later.

How can i make sure that "http://" is NOT in the form? or to take it away if it is.

 

Oh and while im at it.

How can i make the error messages more flexible so to say..

like $error

So i can put it next to the URL form, showing what exactly whats wrong.

Though i was clear... The goal is to uppdate the database.

 

Rank

Rank2

URL

Target

Name

111

222

www.url.com

_Blank

URL.com

2111

2222

www.url.com

_Blank

URL.com

3111

3222

www.url.com

_blank

URL.com

SUBMIT

 

Thats basicly what the page will show. A table with the information (alltho on the website its allso a form)

 

So when i submit (after making a change), for example "Rank" 111 to 555

I wont update.

 

If i change "Rank" 3111 to rank 5555 it will update "Rank" 111 (i think...) anyway only one row is updated instead of all.

 

Oh and well, the ranks are not important, they are just used to "sort" the data, the result it supposed to be a link  ... a href="www.url.com" target="_blank">  name  /a

 

Dont know if i can explain better...

I guess another option is to make a list and then select one link at a time to edit.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.