php update form... Gået helt kold
Jeg har nu prøvet i 6 timer at få min edit profil side til at virke, men nej.Jeg har godt nok fået et script til at køre, men ikke sammen med min edit side.
Jeg prøver at få det at virke med en "if exist update else update" ting.
Har prøvet med "INSERT ON DUPLICATE KEY UPDATE", og med "
if($result) {
if(mysql_num_rows($result) == 1) {
$sql="UPDATE INTO table (value1, value2, value3, value4...) VALUES ('$value1', '$value2', '$value3', '$value4')";
exit();
}else {
$sql="INSERT INTO table (value1, value2, value3, value4...) VALUES ('$value1', '$value2', '$value3', '$value4')";
exit();
}
}else {
die("Query failed");
}"
Samt flere andre, men "No cigar"
Har ikke sat mit update.php ind da intet virker på det
Under her:
profile.php
edit.php
det jeg fik til at virke ja.php
Code
profile.php
<?php
require_once('required/auth.php');
$id = $_GET['id'];
require_once('required/datalogin.php');
// Get a specific result from the "example" table
$result = mysql_query("SELECT * FROM members
WHERE member_id='$id'") or die(mysql_error());
// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Profile</title>
</head>
<body>
<p>Profile</p>
<p> </p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="20%"> </td>
<td width="60%"><table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="25%">Fornavn:</td>
<td width="75%"><?php echo $row['firstname'];?></td>
</tr>
<tr>
<td>Efternavn:</td>
<td><?php echo $row['lastname'];?></td>
</tr>
<tr>
<td height="71">Adresse:</td>
<td><?php echo $row['address'];?></td>
</tr>
<tr>
<td>Første tlf.</td>
<td><?php echo $row['mobile'];?></td>
</tr>
<tr>
<td>Anden tlf.</td>
<td><?php echo $row['mobile2'];?></td>
</tr>
<tr>
<td>Tredje tlf.</td>
<td><?php echo $row['mobile3'];?></td>
</tr>
<tr>
<td>Første e-mail:</td>
<td><?php echo $row['email'];?></td>
</tr>
<tr>
<td>Anden e-mail:</td>
<td><?php echo $row['email2'];?></td>
</tr>
<tr>
<td>Fødselsdag:</td>
<td><?php echo $row['bday'];?> - <?php echo $row['bmonth'];?> - <?php echo $row['byear'];?></td>
</tr>
<tr>
<td> </td>
<td><a href="edit.php?id=<?php echo $id;?> ">Edit</a></td>
</tr>
</table></td>
<td width="20%"> </td>
</tr>
</table>
<p> </p>
</body>
</html>
edit.php
<?php
require_once('required/auth.php');
require_once('required/datalogin.php');
$id = $_GET['id'];
$thispage = $_SERVER['PHP_SELF'];
// Get a specific result from the "example" table
$result = mysql_query("SELECT * FROM members
WHERE member_id='$id'") or die(mysql_error());
// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit profile</title>
</head>
<body>
<p>Edit Profile</p>
<p> </p>
<form name="form1" method="post" action="edit.php">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="20%"> </td>
<td width="60%"><table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="25%">Fornavn:</td>
<td width="75%"><input name="firstname" type="text" id="firstname" value="<?php echo $row['firstname'];?>"></td>
</tr>
<tr>
<td>Efternavn:</td>
<td>
<input name="lastname" type="text" id="lastname" value="<?php echo $row['lastname'];?>"></td>
</tr>
<tr>
<td>Adresse:</td>
<td>
<textarea name="address" id="address" cols="30" rows="3"><?php echo $row['address'];?></textarea></td>
</tr>
<tr>
<td>Første tlf.</td>
<td>
<input name="mobile" type="text" id="mobile" value="<?php echo $row['mobile'];?>"></td>
</tr>
<tr>
<td>Anden tlf.</td>
<td>
<input name="mobile2" type="text" id="mobile2" value="<?php echo $row['mobile2'];?>"></td>
</tr>
<tr>
<td>Tredje tlf.</td>
<td>
<input name="mobile3" type="text" id="mobile3" value="<?php echo $row['mobile3'];?>"></td>
</tr>
<tr>
<td>Første e-mail:</td>
<td>
<input name="email" type="text" id="email" value="<?php echo $row['email'];?>"></td>
</tr>
<tr>
<td>Anden e-mail:</td>
<td>
<input name="email2" type="text" id="email2" value="<?php echo $row['email2'];?>"></td>
</tr>
<tr>
<td>Fødselsdag:</td>
<td><input name="bday" type="text" id="bday" value="<?php echo $row['bday'];?>" size="2">
-
<input name="bmonth" type="text" id="bmonth" value="<?php echo $row['bmonth'];?>" size="2">
-
<input name="byear" type="text" id="byear" value="<?php echo $row['byear'];?>" size="4"></td>
</tr>
<tr>
<td>Ny adgangskode:</td>
<td><input type="password" name="newpasswd" id="newpasswd"></td>
</tr>
<tr>
<td>Gentag Ny adgangskode:</td>
<td><input type="text" name="newpasswd2" id="newpasswd2"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input name="modified" type="hidden" id="modified" value="<?php echo date("Y-m-d H:i:s");?>"></td>
<td><input type="submit" name="update" id="update" value="Update"></td>
</tr>
</table></td>
<td width="20%"> </td>
</tr>
</table>
</form>
<p><a href="test.php">Back</a></p>
</body>
</html>
ja.php
<?php
$id = $_GET['id'];
require 'required/datalogin.php'; // Forbindelse til databasen
$tabel = 'members';
$thispage = $_SERVER['PHP_SELF'];
$res = mysql_query("SHOW COLUMNS FROM $tabel");
while ($row = mysql_fetch_assoc($res)) {
if ($row['Key'] == 'PRI') $pkey = $row['Field']; // Navn på primærnøgle
}
if (isset($_POST['submit'])) {
foreach ($_POST as $key=>$val) {
if ($key != $pkey && $key != 'submit') {
$str[] = "$key='$val'";
}
}
$qstr = join(',', $str);
mysql_query("update $tabel set $qstr where $pkey='$_POST[$pkey]'");
}
$res = mysql_query("select * from $tabel WHERE member_id='$id'");
while ($row = mysql_fetch_assoc($res)) {
echo "<form action='$thispage' method='post'><table>";
foreach ($row as $key=>$val) {
echo "<tr><td><label for='$key'>$key:</label></td><td><input ";
if ($key == $pkey) {echo "readonly='1' ";}
echo "type='text' name='$key' id='$key' value='$val' /></td></tr>";
}
echo "<tr><td><input type='submit' name='submit' value='Opdater' /></td></tr></table></form>";
}
?>
<?
echo "<a href="edit.php?id="$id">Edit</a>";
?>