Mysql vraagje.
In mijn database heb ik de volgende 2 tabellen:
+Units
-id
+unitmembers
-unitid
-userid
Nou gebruik ik deze query als er een team gemaakt word door iemand:
Code (php)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
<?php
$query = "
INSERT INTO
units (`name`, `tag`, `joindate`, `website`, `logo`, `unitemail`)
VALUES ('" . $_POST['name'] . "','" . $_POST['tag1'] . "','" . $joindate . "','" . $_POST['website'] . "','" . $_POST['logo'] . "','" . $_POST['email1'] . "')";
mysql_query($query);
?>
$query = "
INSERT INTO
units (`name`, `tag`, `joindate`, `website`, `logo`, `unitemail`)
VALUES ('" . $_POST['name'] . "','" . $_POST['tag1'] . "','" . $joindate . "','" . $_POST['website'] . "','" . $_POST['logo'] . "','" . $_POST['email1'] . "')";
mysql_query($query);
?>
Maar wat ik nou graag zou willen is dat de userid en unitid in "unitmembers" worden gezet.
dus: hij maakt eerst de unit aan in de database en daarna wil ik graag dat het unitid word opgehaald van eht gemaakte unit en dit in die andere tabel word gezet.
Kan iemand mij hier meschien bij helpen?
Mvg
PHPerik:
Je zal een tweede query (en derde) moeten maken (lang leve mysql) en moeten werken met:
http://www.php.net/mysql-insert-id
http://www.php.net/mysql-insert-id
Ok maar als ik die mysql-insert-id gebruik telt hij er dan automatisch 1 bij op?
Code (php)
1
2
3
4
5
2
3
4
5
<?php
Parse error: parse error in /home/httpd/vhosts/battle-hq.com/subdomains/leagues/httpdocs/graw/functions/join_ladder.php on line 47
Fatal error: Call to undefined function: joinladder() in /home/httpd/vhosts/battle-hq.com/subdomains/leagues/httpdocs/graw/join.php on line 12
?>
Parse error: parse error in /home/httpd/vhosts/battle-hq.com/subdomains/leagues/httpdocs/graw/functions/join_ladder.php on line 47
Fatal error: Call to undefined function: joinladder() in /home/httpd/vhosts/battle-hq.com/subdomains/leagues/httpdocs/graw/join.php on line 12
?>
Weet iemand meschien waar dit aan ligt?
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
require_once("layout.php");
require_once("./config/config.php");
//Unit create date
$joindate = date("d-m-Y H:i");
//Create unit fields
function joinladder(){
global $joindate;
global $homeurl;
global $sitename;
global $tag;
global $email;
top();
tableheader("<font class='menutitle'>Create a unit</font>");
if(isset($_SESSION['callsign'])){
$query = mysql_query("SELECT userid FROM unitmembers WHERE userid = " . $_SESSION['id'] . "");
$result = mysql_num_rows($query);
if($result){
echo'You can not join a second team';
}
else{
if(isset($_POST['submit'])){
//Field check for enters
if (empty($_POST['name']))
{
error_message("No unit name entered.");
}
if (empty($_POST['tag1']))
{
error_message("No unit tag entered.");
}
if (empty($_POST['email1']))
{
error_message("No unit e-mail entered.");
}
else{
$aa = "INSERT INTO units (`name`, `tag`, `joindate`, `website`, `logo`, `unitemail`)
VALUES ('" . addslashes($_POST['name'] . "','" . addslashes($_POST['tag1'] . "','" . $joindate . "','" . addslashes($_POST['website'] . "','" . addslashes($_POST['logo'] . "','" . addslashes($_POST['email1'] . "')";
$rr = mysql_query($aa) or die(mysql_error());
$var = mysql_insert_id();
$jj = "INSERT INTO unitmembers ('userid', 'unitid') VALUES ('".$_SESSION['id']."', '".$var."')";
$pp = mysql_query($jj) or die(mysql_error());
if ($rr, $pp)
{
$aan = $_POST['email1'];
$subject = "" . $tag . "Unit Creating";
$van = "" . $tag . "Webmaster <" . $email . ">";
$bericht = "===========================================\n";
$bericht .= "" . $tag . "Account activation\n";
$bericht .= "===========================================\n\n";
$bericht .= "Your unit has been created\n";
$bericht .= "Your Unit Name: " . $_POST['name'] . "\n";
$bericht .= "Your Unit tag: " . $_POST['tag1'] . "\n\n";
$bericht .= "Thanks for creating a unit on " . $sitename . ".\n\n";
$bericht .= "===========================================\n";
$bericht .= "End of automatic generated message\n";
$bericht .= "===========================================";
// kijk of de e-mail verstuurd kan worden
if (mail($aan, $subject, $bericht, "From: $van"))
{
echo "<p>Your account has been created but you cant login yet.</p>";
echo "<p>There is an e-mail sended whit an activation key";
echo "to activate your account.</p>";
}
}
}
}
echo'
<form action= "join.php?action=joinladder" method="post">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td width="189">Unit name :</td>
<td width="189"><input name="name" type="text" id="name" /></td>
</tr>
<tr>
<td>Unit tag :</td>
<td><input name="tag1" type="text" id="tag1" /></td>
</tr>
<tr>
<td>Unit e-mail : </td>
<td><input name="email1" type="text" id="email1" /></td>
</tr>
<tr>
<td>Unit logo : </td>
<td><input name="logo" type="text" id="logo" /></td>
</tr>
<tr>
<td>Unit website : </td>
<td><input name="website" type="text" id="website" /></td>
</tr>
<tr>
<td colspan="2"><div align="center"><input name="submit" type="submit" id="submit" value="Create Unit" /></div></td>
</tr>
</table>
</form>';
tablefooter();
bottum();
}
}
else{
echo"You have to login";
}
}
?>
require_once("layout.php");
require_once("./config/config.php");
//Unit create date
$joindate = date("d-m-Y H:i");
//Create unit fields
function joinladder(){
global $joindate;
global $homeurl;
global $sitename;
global $tag;
global $email;
top();
tableheader("<font class='menutitle'>Create a unit</font>");
if(isset($_SESSION['callsign'])){
$query = mysql_query("SELECT userid FROM unitmembers WHERE userid = " . $_SESSION['id'] . "");
$result = mysql_num_rows($query);
if($result){
echo'You can not join a second team';
}
else{
if(isset($_POST['submit'])){
//Field check for enters
if (empty($_POST['name']))
{
error_message("No unit name entered.");
}
if (empty($_POST['tag1']))
{
error_message("No unit tag entered.");
}
if (empty($_POST['email1']))
{
error_message("No unit e-mail entered.");
}
else{
$aa = "INSERT INTO units (`name`, `tag`, `joindate`, `website`, `logo`, `unitemail`)
VALUES ('" . addslashes($_POST['name'] . "','" . addslashes($_POST['tag1'] . "','" . $joindate . "','" . addslashes($_POST['website'] . "','" . addslashes($_POST['logo'] . "','" . addslashes($_POST['email1'] . "')";
$rr = mysql_query($aa) or die(mysql_error());
$var = mysql_insert_id();
$jj = "INSERT INTO unitmembers ('userid', 'unitid') VALUES ('".$_SESSION['id']."', '".$var."')";
$pp = mysql_query($jj) or die(mysql_error());
if ($rr, $pp)
{
$aan = $_POST['email1'];
$subject = "" . $tag . "Unit Creating";
$van = "" . $tag . "Webmaster <" . $email . ">";
$bericht = "===========================================\n";
$bericht .= "" . $tag . "Account activation\n";
$bericht .= "===========================================\n\n";
$bericht .= "Your unit has been created\n";
$bericht .= "Your Unit Name: " . $_POST['name'] . "\n";
$bericht .= "Your Unit tag: " . $_POST['tag1'] . "\n\n";
$bericht .= "Thanks for creating a unit on " . $sitename . ".\n\n";
$bericht .= "===========================================\n";
$bericht .= "End of automatic generated message\n";
$bericht .= "===========================================";
// kijk of de e-mail verstuurd kan worden
if (mail($aan, $subject, $bericht, "From: $van"))
{
echo "<p>Your account has been created but you cant login yet.</p>";
echo "<p>There is an e-mail sended whit an activation key";
echo "to activate your account.</p>";
}
}
}
}
echo'
<form action= "join.php?action=joinladder" method="post">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td width="189">Unit name :</td>
<td width="189"><input name="name" type="text" id="name" /></td>
</tr>
<tr>
<td>Unit tag :</td>
<td><input name="tag1" type="text" id="tag1" /></td>
</tr>
<tr>
<td>Unit e-mail : </td>
<td><input name="email1" type="text" id="email1" /></td>
</tr>
<tr>
<td>Unit logo : </td>
<td><input name="logo" type="text" id="logo" /></td>
</tr>
<tr>
<td>Unit website : </td>
<td><input name="website" type="text" id="website" /></td>
</tr>
<tr>
<td colspan="2"><div align="center"><input name="submit" type="submit" id="submit" value="Create Unit" /></div></td>
</tr>
</table>
</form>';
tablefooter();
bottum();
}
}
else{
echo"You have to login";
}
}
?>
Je bent bij alle addslashes() het aflsuitende haakje ) vergeten
$joindate = date("d-m-Y H:i");
Daar ga je nog de nodige problemen mee krijgen, dan weet je dat vast.
Een datum sla je altijd op in een veld van het type DATE of DATETIME. En die staan in MySQL in het formaat yyyy-mm-dd en dan evt. nog de tijd. Met dd-mm-yyyy sla je de plank dus volledig mis. Ga maar eens sorteren op datum, dan krijg je de meest bijzondere resultaten.
Ok bedankt voor de tips!