Jump to content

trying to access id from mysql dynamically


rahulvicky00

Recommended Posts

I am trying to make an edit page to edit any of my post...so i designed a manage post page manage-posts.php with the given code:

 

    <?php 
echo '<form name="frmMain" action="del1.php" method="post" OnSubmit="return onDelete();">';
			$objConnect = mysql_connect("hostname","username","password") or die(mysql_error());  
    $objDB = mysql_select_db("dbname");
    $strSQL = "SELECT * FROM text";  
    $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");  
     
echo '<table width="600" border="1">';  
    echo '<tr>';  
   echo '<th width="91"> <div align="center">ID</div></th>'; 
echo '<th width="91"> <div align="center">Date</div></th>';  
    echo '<th width="91"> <div align="center">Title</div></th>';
echo '<th width="91"> <div align="center">Author</div></th>';
    echo '<th width="30"> <div align="center">Edit</div></th>';
echo '<th width="30"> <div align="center">Select</div></th>';
    echo '</tr>'; 
   		while($objResult = mysql_fetch_array($objQuery))  
    			{  
   	  		?>
   	 <tr>  
    <td><?=$objResult["id"];?></td>
    <td><?=$objResult["date"];?></td>  
    <td><?=$objResult["title"];?></td>  
    <td><div align="center"><?=$objResult["author"];?></div></td>  
<td align="center"><a href="edit.php?NewsID=<?php echo $objResult["id"];?>" name="edit">Edit</a></td>   
    <td align="center"><input type="checkbox" name="chkDel[]" value="<?=$objResult["id"];?>"></td>  
    <input type="hidden" name="id" value="<?=$objResult["id"];?>" />
    </tr>  
     <?
      }  
    
    echo '</table>'; 
echo '<input type="submit" name="btnDelete" value="Delete">';  
    echo '</form>'; 

 

and i designed another page edit.php to perform deletion of that particular post with the following code "

<?php 

			$objConnect = mysql_connect("hostname","username","pass") or die(mysql_error());  
    $objDB = mysql_select_db("dbname");
  
   $strSQL = "SELECT * FROM text";  
    $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");  
$objResult = mysql_fetch_array($objQuery);
?>
  			<input type="hidden" name="id" value="<?=$objResult["id"];?>" />
			Title : <br /><input type="text" name="title" size="100"  maxlength="100" value="<?=$objResult["title"];?>"/> <br />
            Date : <br /><input type="text" name="date" size="20"  maxlength="12" id="TextBox" value="<?=$objResult["date"];?>"/> <br />
            Author : <br /><input type="text" name="author" size="20"  maxlength="100" value="<?=$objResult["author"];?>"/> <br />
            <br />
<input type="submit" value="Submit" name="submit" />
            </p>

</form></fieldset></div>

 

 

But the problem is that i am not able to get that particular post every time whenever i clicked on the respective post's edit link..

i suppose there is any issue in calling the id from the mysql... kindly suggest solutions... thanks in advance...

Link to comment
Share on other sites

In the edit file, you're not telling the SQL to search for the post you selected to edit:

 

$strSQL = "SELECT * FROM text";

 

Should be:

 

$strSQL = "SELECT * FROM text WHERE id=$_GET[newsID]";

 

But that's also incorrect, because you should validate $_GET['newsID'] before you run it in an SQL, otherwise you leave yourself vulnerable to SQL injection or other abuse.

 

One method would be to static cast the ID as an integer, validate that it's a valid ID (possibly greater than 0?), and then use it in the SQL.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.