Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

storing & retrieving data

Status
Not open for further replies.

7724

Programmer
Feb 25, 2005
28
0
0
GB
Hi Guys,

I'm nearly there;

I have a form with the following code;

Code:
<?php

// login stuff

include("");
include("");
checklogin();

$msg = "";
$name = "";
$heading = "";
$position = "";
$signed = "";
$debut = "";
$dob = "";
$body = "";

if(isset($_POST['Submit']))
{

	$name = $_POST['name'];
	$heading = $_POST['heading'];
	$position = $_POST['position'];
	$signed = $_POST['signed'];
	$debut = $_POST['debut'];
	$goal = $_POST['goal'];
	$dob = $_POST['dob'];
	$body = $_POST['body'];
	
	if(!isset($_GET['profilesid']))
	{
		$result = mysql_query("Insert into profiles(name,heading,position,signed,debut,goal,dob,body) values('$name','$heading','$position','$signed','$debut','$goal','$dob','$body')");
		$msg = "New record is saved";
	}
	else
	{
		$result = mysql_query("Update profiles set name='$name', heading='$heading', position='$position', signed='$signed', debut='$debut', goal='$goal', dob='$dob', body='$body' where profilesid=".$_GET['profilesid']);
		$msg = "profiles Record is updated";
	}
}
if(isset($_GET['profilesid']))
{
	$result = mysql_query("Select * From profiles where profilesid=".$_GET['profilesid'],$link);
	$row = mysql_fetch_array($result, MYSQL_BOTH);
	$name = $row['name'];
	$heading = $row['heading'];
    $position = $row['position'];
    $signed = $row['signed'];
    $debut = $row['debut'];
    $goal = $row['goal'];
    $dob = $row['dob'];
    $body = $row['body'];
}
?>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<form name="form1" method="post" action="">
<table width="100%" border="0">
  <tr> 
    <td bgcolor="#FFFF00"><?php echo $msg?></td>
    <td>&nbsp;</td>
  </tr>
  <tr> 
    <td>Name of Author:</td>
    <td> 
      <select class="input" name="name" id="name">
        <option value="Person 1"<? if ($name == 'Person 1') echo ' selected' ?>>Person 1
        <option value="Person 2"<? if ($name == 'Person 2') echo ' selected' ?>>Person 2 
      </select>
    </td>
  </tr>
  <tr> 
    <td>Player Name:</td>
    <td> 
      <input name="heading" type="text" id="heading" value="<?php echo $heading?>" size="36">
    </td>
  </tr>
  <tr> 
    <td>Position:</td>
    <td> 
      <input name="position" type="text" id="position" value="<?php echo $position?>" size="36">
    </td>
  </tr>
  <tr> 
    <td>Signed from:</td>
    <td> 
      <input name="signed" type="text" id="signed" value="<?php echo $signed?>" size="36">
    </td>
  </tr>
  <tr> 
    <td>Debut:</td>
    <td> 
      <input name="debut" type="text" id="debut" value="<?php echo $debut?>" size="36">
    </td>
  </tr>
  <tr> 
    <td>First Goal:</td>
    <td> 
      <input name="goal" type="text" id="goal" value="<?php echo $goal?>" size="36">
    </td>
  </tr>
  <tr> 
    <td>Date of Birth:</td>
    <td> 
      <input name="dob" type="text" id="dob" value="<?php echo $dob?>" size="36">
    </td>
  </tr>
  <tr> 
    <td>Body Text:</td>
    <td> 
      <textarea name="textarea" id="body" cols="60" rows="20"><?php echo $body?></textarea>
    </td>
  </tr>
  <tr> 
    <td>Select an Image:</td>
      <td> <?php
if (isset($_POST['image'])){
    $selected = $_POST['image'];
    $selected_display = $_POST['image'];
} else { 
    $selected = "";
    $selected_display = "select an image...";
}
echo "
      <select name='image'> 
      <option selected value='$selected'> $selected_display </option> 
";
$query = mysql_query("SELECT * FROM `uploads`"); 
while($reza = mysql_fetch_assoc($query)){
$name = $reza['name']; 
$location = $reza['url']; 
echo " <option value='$location'>$name</option>";
} 
echo "</select>";
?></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td> 
      <div align="right">
        <input type="submit" name="Submit" value="Submit" class="button">
        <input type="reset" name="Reset" value="Reset" class="button">
      </div>
    </td>
  </tr>
</table>
</form>
</body>
</html>

Now when I hit submit the text data is saved to the profiles table (fine and what I want) and a new record is created, but the image dropdown data i selected from the uploads table is not held.

What I want to do is go into each record when it is saved and see what image (from the dropdown) has been assigned.

Currently when I select an image from the dropdown hit submit, then go back later to modify it the dropdown data has not been held and it just says 'select an image' when I want the inital value I selected to be showing when I go to modify it.

Any ideas pleeaasee??

Many Thanks

Chris
 
See if this works out any better for you.

PHP:
 <tr>
    <td>Select an Image:</td>
      <td> 
<?php
   if (!isset($_POST['image'])){
     echo "<option selected value=''>select an image...</option>";
} else {
 
 echo "<option value='$selected'> $selected_display </option>";
}
$selected = $_POST['image'];
$query = mysql_query("SELECT * FROM `uploads`");
while($reza = mysql_fetch_assoc($query)){
$name = $reza['name'];
$location = $reza['url'];
echo "<option value='$location'";
if ($location == $selected) {
  echo " selected";
}// end of selection check
echo " >$name</option>";
}  // end of while loop
echo "</select>";
?></td>
  </tr>
 
Sorry I messed up on the first one. Here are the changes.

<tr>
<td>Select an Image:</td>
<td>
<?php
$selected = "";
if (!isset($_POST['image'])){
echo "<option selected value=''>select an image...</option>";
} else {
echo "<option value=''>select an image...</option>";
$selected = $_POST['image'];
}
$query = mysql_query("SELECT * FROM `uploads`");
while($reza = mysql_fetch_assoc($query)){
$name = $reza['name'];
$location = $reza['url'];
echo "<option value='$location'";
if ($location == $selected) {
echo " selected";
}// end of selection check
echo " >$name</option>";
} // end of while loop
echo "</select>";
?></td>
</tr>
 
Also, I have some things to point out here.
Your script is not very "safe".

Please read the example here:
(example 2. - valid query).

You would also want to strip unwanted tags with:
strip_tags() and trim() to trim away whitespace.

I think that after that, you should be proud of your self and drink some coffee, or how you spell that word..

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top