Editor
Ik heb een MySQL database met ong. 300 records, nu wil ik een script bouwen dat de id laad die ik in een veld in typ en dat ik dan de record gegevens zie en dan 2 dingen aan het record kan aanpassen. Hoe moet ik dit doen?
Die code ziet er ongeveer als volgt uit
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php require_once('Connections/contact.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_contact, $contact);
$query_Record_set_1 = "SELECT id, username, password FROM users";
$Record_set_1 = mysql_query($query_Record_set_1, $contact) or die(mysql_error());
$row_Record_set_1 = mysql_fetch_assoc($Record_set_1);
$totalRows_Record_set_1 = mysql_num_rows($Record_set_1);
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_contact, $contact);
$query_Record_set_1 = "SELECT id, username, password FROM users";
$Record_set_1 = mysql_query($query_Record_set_1, $contact) or die(mysql_error());
$row_Record_set_1 = mysql_fetch_assoc($Record_set_1);
$totalRows_Record_set_1 = mysql_num_rows($Record_set_1);
?>
Hierin moet je alleen wel de tweede regel aanpassen naar de plaats waar je de gegevens hebt staan om verbinding met de database te maken en verder moet je zorgen dat de recordset en dat het update script de gegevens uit de juiste tabellen haalt.
Bij het formulier heb je dan volgende code.
LET OP: ik heb snel een formulier gebruikt van een pagina die ik gemaakt heb, je moet dus het formulier zelf even aanpassen.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<form class="register active" method="POST" name="register">
<h3>Register</h3>
<div class="column">
<div>
<label>id</label>
<input name="id" type="text" placeholder="id" value="" />
</div>
<div>
<label>Username:</label>
<input name="username" type="text" placeholder="Gebruikersnaam" value="<?php echo $row_Record_set_1['username']; ?>"/>
<span class="error">This is an error</span>
</div>
<div>
<label>Password:</label>
<input name="password" type="password" placeholder="Wachtwoord" value="<?php echo $row_Record_set_1['password']; ?>" />
<span class="error">This is an error</span>
</div>
</div>
<div class="bottom">
<div class="remember">
<input name="updates" value="1" type="checkbox" />
<span>Send me updates</span>
</div>
<input type="submit" value="Register" />
<a href="beheer/index.php" rel="login" class="linkform">You have an account already? Log in here</a>
<div class="clear"></div>
</div>
</form>
<h3>Register</h3>
<div class="column">
<div>
<label>id</label>
<input name="id" type="text" placeholder="id" value="" />
</div>
<div>
<label>Username:</label>
<input name="username" type="text" placeholder="Gebruikersnaam" value="<?php echo $row_Record_set_1['username']; ?>"/>
<span class="error">This is an error</span>
</div>
<div>
<label>Password:</label>
<input name="password" type="password" placeholder="Wachtwoord" value="<?php echo $row_Record_set_1['password']; ?>" />
<span class="error">This is an error</span>
</div>
</div>
<div class="bottom">
<div class="remember">
<input name="updates" value="1" type="checkbox" />
<span>Send me updates</span>
</div>
<input type="submit" value="Register" />
<a href="beheer/index.php" rel="login" class="linkform">You have an account already? Log in here</a>
<div class="clear"></div>
</div>
</form>
Hopelijk heb je hier iets aan ;)