PDA

View Full Version : MySQL Connection


globalwin
07-31-2002, 04:53 PM
Hi people, can anyone tell me how I would connect to my DB, to a table called domains, and add data to it, then disconnect, the database is also called domains, and the user is root.

Admin
07-31-2002, 05:23 PM
Roughly:
<?php

$cid = mysql_connect('localhost', 'root', 'PASSWORD');
mysql_select_db('domains', $cid);

mysql_query('
INSERT INTO domains
SET field1 = "somestring", field2 = 14, field3 = "someotherstring
');

mysql_close($cid);

?>

globalwin
07-31-2002, 05:24 PM
cheers, :D

globalwin
07-31-2002, 05:43 PM
come to try it, but i failed :(, here is my code:


<?php

$cid = mysql_connect('localhost', 'root', '');
mysql_select_db('domains', $cid);

mysql_query('
INSERT INTO domains
SET field1 = "fname", field2 = "lname", field3 = "email", field4 = "domain", field5 = "length", field6 = "address"
');

mysql_close($cid);

?>


and my form:


<form action="db.php" method="post" enctype="multipart/form-data" name="domains" target="_self" class="bodytext" id="domains">
<table width="525" border="0">
<tr>
<td>First Name:</td>
<td><input type="text" name="fname" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lname" /></td>
</tr>
<tr>
<td>Email Address:</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Domain Name:</td>
<td><input type="text" name="domain" /></td>
</tr>
<tr>
<td><p>Registration Years:</p>
</td>
<td><input type="text" name="length" /></td>
</tr>
<tr>
<td valign="top">Mailing Address:</td>
<td><textarea name="address" cols="40" rows="5"></textarea></td>
</tr>
</table>
<p>* - All Fields Required</p>
<p>
<input type="submit" name="Submit" value="Submit" />
<input type="reset" name="Reset" value="Reset" />
</p>
</form>

Admin
07-31-2002, 05:49 PM
<?php

$cid = mysql_connect('localhost', 'root', '');
mysql_select_db('domains', $cid);

$fields = array('fname', 'lname', 'email', 'domain', 'length', 'address');

$fieldlist = '';
foreach ($fields as $field) {
if (!empty($fieldlist)) {
$fieldlist .= ', ';
}
$fieldlist .= "$field = '".addslashes($_POST["$field"])."'";
}

mysql_query("
INSERT INTO domains
SET $fieldlist
");

mysql_close($cid);

?>

globalwin
07-31-2002, 05:57 PM
YAY, data got inserted into the DB, it was all 0 though, can anyone please tell me what type of field i want because I am using TINYINTs at the min and it doesnt work. Cheers Chen. :D

Admin
07-31-2002, 06:03 PM
You need to use VARCHAR (or CHAR) for string types, and TINYINT / SMALLINT / INT for integers.

globalwin
07-31-2002, 06:17 PM
ok, getting there and thanx for the help, now, as before i could see 0's, not MySQL Front says the data is there, but I cant view it.

Admin
07-31-2002, 06:19 PM
You will need to enter the data again.

globalwin
07-31-2002, 06:23 PM
hehe :D, is there anyway I can make the page display a redirection note like vB and then take them back to my homepage? Thanks for the intense help! :)