Jump to content


Photo

php/mysql problem...


  • Please log in to reply
16 replies to this topic

#1 jack12345

jack12345
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 26 July 2006 - 10:03 PM

I have a small problem in regards to php/mysql. I have a drop down box/list on my form for entering data into a mysql database.

<select>
<?php

$options = array('Jack', 'Jane', 'Irene', 'Marry', 'Jody', 'Ana', 'Ivana');
foreach ($options as $option) {
    $isSelected = $option == $name ? ' selected' : '';
    print "<option$isSelected>$option</option>\n";
}

?>
</select>

Now the problem is that instead of insertin Irene, Jane, Marry... into the database i'd like to insert html code in it. Example <a href="url">Irene</a> and something like that for every name. Now i can't seem to figure out how to use that instead of just the name. Any help would be appreciated.

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 26 July 2006 - 10:26 PM

Where is the code that does the actual inserting?

#3 jack12345

jack12345
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 26 July 2006 - 10:38 PM

I'm not aure i understand the question exactly. But this drop down box. When i select an option from it and click add to database it get's added so i don't have a problem with it. It is a part of a form which works ok.

The problem is that i am not able to replace Irene, Jane, Jack ... which are in the array as they are they are not variables, with html code. So that when i select Jack in the database it get's added <a href="http://www.test.com/....html">Jack</a> So how do i replace those simple names in the array with html code.

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 26 July 2006 - 11:57 PM

try this ok save as seen lol.......................

edited with database info ok.

when a user selects a username the database will get the link with the user name and url.
test.php



<form action="" method="POST">

<select name="name">

<?php

$array=array('Jack', 'Jane', 'Irene', 'Marry', 'Jody', 'Ana', 'Ivana');

for($i=0; $i<count($array); $i++){

?>

<option value=<? echo $array[$i]; ?> >

<?echo $array[$i]; ?>

</option>

<?}?>
<input type="submit" name="submit" value="submit">

</select>

<?

if($_POST['submit']){

$member_name_with_link="<a href='what_ever.com'>$name</a>";

$query="INSERT INTO xxxxx ( member_name_with_link)VALUES('$member_name_with_link')";

$result=mysql_query($query);
}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 27 July 2006 - 12:07 AM

Now the problem is that instead of insertin Irene, Jane, Marry... into the database i'd like to insert html code in it.


Once again. Can we see the code that does the actual database insert?

#6 jack12345

jack12345
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 July 2006 - 12:17 AM

Ok here is the whole code of the form.

<html>

<body>



<?php



$db = mysql_connect("localhost", "dbuser", "dbpass");

mysql_select_db("dbname",$db);



if ($submit) {

  // here if no ID then adding else we're editing

  if ($id) {

    $sql = "UPDATE profile SET name='$name',listassignement='$listassignement',listposition='$listposition',rank_image='$rank_image', medal_image='$medal_image', rank='$rank', species='$species', birthplace='$birthplace', current_posting='$current_posting', qualification='$qualification', age='$age', sex='$sex', dob='$dob', appearance='$appearance',history='$history', background='$background', service_record='$service_record', commendations='$commendations', reprimands='$reprimands', med_record='$med_record', interests='$interests', added_notes='$added_notes', picture='$picture' WHERE id=$id";

  } else {

    $sql = "INSERT INTO profile (name,listassignement,listposition,rank_image,medal_image,rank,species,birthplace,current_posting,qualification,age,sex,dob,appearance,history,background,service_record,commendations,reprimands,med_record,interests,added_notes,picture) VALUES ('$name','$listassignement','$listposition','$rank_image','$medal_image','$rank','$species','$birthplace','$current_posting','$qualification','$age','$sex','$dob','$appearance','$history','$background','$service_record','$commendations','$reprimands','$med_record','$interests','$added_notes','$picture')";

  }

  // run SQL against the DB

  $result = mysql_query($sql) or die(mysql_error());
  

  echo "Record updated/edited!<p>";

} elseif ($delete) {

	// delete a record

    $sql = "DELETE FROM profile WHERE id=$id";	

    $result = mysql_query($sql);

    echo "$sql Record deleted!<p>";

} else {

  // this part happens if we don't press submit

  if (!$id) {

    // print the list if there is not editing

    $result = mysql_query("SELECT * FROM profile",$db);

    while ($myrow = mysql_fetch_array($result)) {

      printf("<a href=\"%s?id=%s\">%s %s</a> \n", $PHP_SELF, $myrow["id"], $myrow["name"], $myrow["rank"]);

	  printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["id"]);

    }

  }



  ?>

  <P>

  <a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>

  <P>

  <form method="post" action="<?php echo $PHP_SELF?>">

  <?php



  if ($id) {

    // editing so select a record

    $sql = "SELECT * FROM profile WHERE id=$id";

    $result = mysql_query($sql);

    $myrow = mysql_fetch_array($result);

    $id = $myrow["id"];

    $name = $myrow["name"];

    $listassignement = $myrow["listassignement"];

    $listposition = $myrow["listposition"];

    $rank_image = $myrow["rank_image"];
	
	$medal_image = $myrow["medal_image"];

	$rank = $myrow["rank"];

	$species = $myrow["species"];	

	$birthplace = $myrow["birthplace"];

	$current_posting = $myrow["current_posting"];

	$qualification = $myrow["qualification"];	

	$age = $myrow["age"];

	$sex = $myrow["sex"];

	$dob = $myrow["dob"];

	$appearance = $myrow["appearance"];

	$history = $myrow["history"];

	$background = $myrow["background"];

	$service_record = $myrow["service_record"];

	$commendations = $myrow["commendations"];	

	$reprimands = $myrow["reprimands"];

	$med_record = $myrow["med_record"];

	$interests = $myrow["interests"];

	$added_notes = $myrow["added_notes"];

	$picture = $myrow["picture"];

    // print the id for editing



    ?>

    <input type=hidden name="id" value="<?php echo $id ?>">

    <?php

  }



  ?>

  Character Name:<br><br><input type="Text" name="name" value="<?php echo $name ?>"><br><br>

  Assignement for the positions page(a link to the ship or base on which he is serving):
    <br><br>
    
      <select name="listassignement">
<?php


$options = array('USS Jackson', 'USS Viking', 'USS Delaware', 'USS Big Foot', 'USS Jody', 'USS Ana', 'USS Ivana');
foreach ($options as $option) {
    $isSelected = $option == $listassignement ? ' selected' : '';
    print "<option$isSelected>$option</option>\n";
}

?>
</select>
        <br><br>

  Position for the positions page, use an abrevation, consult the position page for the abrevations<br><br>
  <select name="listposition">
<?php

$options = array('', 'CO', 'FO', 'CTSO', 'SMO', 'CEO', 'COO', 'CNO', 'CScO', 'CMO', 'CNS', 'EXO', 'MLT', 'CTO', 'CSO', 'Tac', 'Sec', 'CIVIL', 'ORE', 'DCS', 'AMB', 'CSbS', 'CSbO', 'ECH', 'LiO', 'CSOO', 'CIO', 'AI', 'Cadet', 'CHO', 'TO', 'SO', 'FW', 'CCO', 'NS', 'CAO');
foreach ($options as $option) {
    $isSelected = $option == $listposition ? ' selected' : '';
    print "<option$isSelected>$option</option>\n";
}

?>
</select>
  <br>
  <br>

  Rank Image(just enter the name of the rank image file something.jpg):<input type="Text" name="rank_image" value="<?php echo $rank_image ?>"><br>

	Medal image(enter the html code to display the medal image):
	<textarea name="medal_image" cols="80" rows="20"><?php echo $medal_image ?></textarea>
	<br>

	Rank name:<input type="Text" name="rank" value="<?php echo $rank?>"><br>

	Species:<input type="Text" name="species" value="<?php echo $species ?>"><br>

	Birthplace:<br>
	<textarea name="birthplace" cols="80"><?php echo $birthplace ?></textarea>
	<br>

	Current Posting:<input type="Text" name="current_posting" value="<?php echo $current_posting ?>"><br>

	Qualification:<br>
	<textarea name="qualification" cols="80" rows="5"><?php echo $qualification ?></textarea>
	<br>

	Age:<input type="Text" name="age" value="<?php echo $age ?>"><br>

	Sex:<input type="Text" name="sex" value="<?php echo $sex ?>"><br>

	Date of Birth:<input type="Text" name="dob" value="<?php echo $dob ?>"><br>

	Apperance:<br><textarea name="appearance" cols="60" rows="20" wrap="VIRTUAL" ><?php echo $appearance ?></textarea>
	<br>
History:<br><textarea name="history" cols="80" rows="20" wrap="VIRTUAL" ><?php echo $history ?></textarea>
	<br>
	

	Background:<br><textarea name="background" cols="80" rows="20" wrap="VIRTUAL" ><?php echo $background ?></textarea>
	<br>

	Service record, enter a (br at the end of each line):<br><textarea name="service_record" cols="60" rows="20" wrap="VIRTUAL" ><?php echo $service_record ?></textarea>
	<br>

	Commendations(same as above):<br><textarea name="commendations" cols="60" rows="20" wrap="VIRTUAL" ><?php echo $commendations ?></textarea>
	<br>

	Reprimands:<br><textarea name="reprimands" cols="60" rows="20" wrap="VIRTUAL" ><?php echo $reprimands ?></textarea>
	<br>

	Medical Record:<br><textarea name="med_record" cols="60" rows="20" wrap="VIRTUAL" ><?php echo $med_record ?></textarea>
	<br>

	Interests:<br><textarea name="interests" cols="60" rows="20" wrap="VIRTUAL" ><?php echo $interests ?></textarea>
	<br>
	
	Additional Notes:<br><textarea name="added_notes" cols="60" rows="20" wrap="VIRTUAL" ><?php echo $added_notes ?></textarea>
	<br>

	Picture(just enter the image url):<br>
	<textarea name="picture" cols="70" rows="2"><?php echo $picture ?></textarea>
	<br>

  <input type="Submit" name="submit" value="Enter information">

  </form>



<?php



}



?>



</body>

</html>


#7 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 27 July 2006 - 12:22 AM

Is it the $listposition variable your talking about? I dont see your original code anywhere.

#8 jack12345

jack12345
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 July 2006 - 12:26 AM

No, it is $listassignement.

#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 27 July 2006 - 12:26 AM

you need to stripslashes and also you need to sort out the php_self
that all i can help with good luck.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 27 July 2006 - 12:29 AM

your using the varable $options a lot
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#11 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 27 July 2006 - 12:30 AM

No, it is $listassignement.


if ($id) {

    $sql = "UPDATE profile SET name='$name',listassignement='<a href=\"url\">$listassignement</a>',listposition='$listposition',rank_image='$rank_image', medal_image='$medal_image', rank='$rank', species='$species', birthplace='$birthplace', current_posting='$current_posting', qualification='$qualification', age='$age', sex='$sex', dob='$dob', appearance='$appearance',history='$history', background='$background', service_record='$service_record', commendations='$commendations', reprimands='$reprimands', med_record='$med_record', interests='$interests', added_notes='$added_notes', picture='$picture' WHERE id=$id";

  } else {

    $sql = "INSERT INTO profile (name,listassignement,listposition,rank_image,medal_image,rank,species,birthplace,current_posting,qualification,age,sex,dob,appearance,history,background,service_record,commendations,reprimands,med_record,interests,added_notes,picture) VALUES ('$name','<a href=\"url\">$listassignement</a>','$listposition','$rank_image','$medal_image','$rank','$species','$birthplace','$current_posting','$qualification','$age','$sex','$dob','$appearance','$history','$background','$service_record','$commendations','$reprimands','$med_record','$interests','$added_notes','$picture')";

  }


#12 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 27 July 2006 - 12:30 AM

Redarrow... do you even read the questions?

#13 jack12345

jack12345
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 July 2006 - 12:39 AM

Ok i've tried that and now i get always the same url no matter what option i select from the drop down box and i need the url to be different for each option. And thnx for taking your time to help me out.

#14 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 27 July 2006 - 12:46 AM

Hoiw do you want the url to be different? Where is this different data comming from? Give me an example url.

#15 jack12345

jack12345
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 July 2006 - 12:55 AM

Ok. I'll explain. I have a drop down box the one where i chose listassignement. There i have several ships listed. Say USS Jackson
      USS Viking
      USS Delaware
so when i select in the list let's say USS Viking in the database it gets's added <a href="http://felixsavali.a.../table.php">USS Viking</a> or if i select USS Jackson it gets added <a href="http://test.freserve...ackson.php">USS Jackson</a>

#16 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 27 July 2006 - 01:33 AM

Give me an example url.

#17 jack12345

jack12345
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 July 2006 - 10:43 AM

Ups... i gave it but i forgot to put it in code so the post read it like a hyperlink. Here it is:
<a href="http://felixsavali.awardspace.com/table.php">USS Viking</a>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users