Jump to content

Archived

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

jack12345

php/mysql problem...

Recommended Posts

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.

[code]<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>[/code]

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.

Share this post


Link to post
Share on other sites
Where is the code that does the actual inserting?

Share this post


Link to post
Share on other sites
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/jack.html">Jack</a> So how do i replace those simple names in the array with html code.

Share this post


Link to post
Share on other sites
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
[code]



<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);
}
?>
[/code]

Share this post


Link to post
Share on other sites
[quote]Now the problem is that instead of insertin Irene, Jane, Marry... into the database i'd like to insert html code in it.[/quote]

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

Share this post


Link to post
Share on other sites
Ok here is the whole code of the form.

[code]<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>[/code]

Share this post


Link to post
Share on other sites
Is it the $listposition variable your talking about? I dont see your original code anywhere.

Share this post


Link to post
Share on other sites
you need to stripslashes and also you need to sort out the php_self
that all i can help with good luck.

Share this post


Link to post
Share on other sites
[quote]No, it is $listassignement.[/quote]

[code=php:0]
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')";

  }
[/code]

Share this post


Link to post
Share on other sites
Redarrow... do you even read the questions?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Hoiw do you want the url to be different? Where is this different data comming from? Give me an example url.

Share this post


Link to post
Share on other sites
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.awardspace.com/table.php">USS Viking</a> or if i select USS Jackson it gets added <a href="http://test.freserver.org/Jackson.php">USS Jackson</a>

Share this post


Link to post
Share on other sites
Give me an example url.

Share this post


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

Share this post


Link to post
Share on other sites

×

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.