/    Sign up×
Community /Pin to ProfileBookmark

form/sql help

I need some help with submitting form data to a db. I have tried so many ways to do this and it seems for what ever reason If I have any success I get a blank record inserted. Here is the code to my form. If anyone could write up an sql connection script with an insert statement and possibly verification to be sure no fields were left empty, it would be truely a huge help.

[code=php]<form name=”step1″ method=”post” action=”basic2.php”>

<table border=”0″ cellpadding=”2″ cellspacing=”0″ width=”100%”>
<tbody><tr>
<td colspan=”2″><b>Please enter a valid e-mail address. You will need to confirm your e-mail address to activate your account.</b>
</td>
</tr>
<tr>
<td class=”required”>Company Name</td>
<td class=”required”><input type=”text” name=”comp_name” size=”20″ maxlength=”15″ /></td>
</tr>
<tr>
<td class=”required”>Establishment Type</td>
<td class=”required”>
<select name=”comp_type”>
<option selected>- Select One -</option>
<option value=”bars”>Bar</option>
<option value=”clubs”>Club</option>
<option value=”coffee_shops”>Coffee Shop</option>
<option value=”resturants”>Resturant</option>
</select>
</td>
</tr>
<tr>
<td class=”required”>Address</td>
<td class=”required”><input type=”text” name=”comp_address” size=”30″ maxlength=”30″ /></td>
</tr>
<tr>
<td class=”required”>Phone Number</td>
<td><input type=”text” name=”comp_phone” size=”20″ maxlength=”40″ /></td>
</tr>
<tr>
<td class=”required”>Email Address</td>
<td><input type=”text” name=”comp_email” size=”30″ maxlength=”20″ /></td>
</tr>
<tr>
<td class=”required”>Website Address</td>
<td><input type=”text” name=”comp_web” size=”30″ maxlength=”20″ /></td>
</tr>
<tr>
<td class=”required”>Description</td>
<td><textarea name=”comp_desc” cols=”30″ rows=”12″></textarea></td>
</tr>
<tr>
<td class=”verdana11″ align=”right” width=”125″><p></td>
<td><input onClick=”basic2.php” type=”submit” name=”submit” value=”Go to step 2″ /></td>
</tr>
</tbody>
</table>

</form>[/code]

Thanks alot for the help

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayNov 26.2007 — Would help if you could post your best attempt at the PHP code....
Copy linkTweet thisAlerts:
@crazy8authorNov 26.2007 — Well I do have a script that has 4 of the attempts I made. Ill save from making to much of a mess so here is one of them...

[code=php]<?php
$username = "root";
$password = "";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("establishment",$dbhandle)
or die("Could not select establishment");

//execute the SQL query and return records
$sql = "INSERT INTO establishment (comp_type, comp_name, comp_address, comp_phone, comp_email, comp_web, comp_desc) VALUES ('$comp_type','$comp_name','$comp_address','$comp_phone','$comp_email','$comp_web','$comp_desc')";

//close the connection
mysql_close($dbhandle);
?>[/code]


Now I am still debaiting if I should do this or store my form data into sessions or not. In anycase with this I just need the form data to be inserted into the db and thats pretty much it.

Thanks
Copy linkTweet thisAlerts:
@NogDogNov 26.2007 — Where do the variables being used in the VALUES() list get set?

To help debug, you might want to echo $sql to see if it looks right or if anything (such as the values) is missing.
Copy linkTweet thisAlerts:
@crazy8authorNov 26.2007 — Where do the variables being used in the VALUES() list get set?[/QUOTE]

Maybe thats why even after I fill the form and hit submit, I get "empty" records inserted into the db. That would make sense.

So to set the variables do I just do something like

[code=php]$comp_type = 'comp_type'; //is that right and correct syntax?[/code]

Thanks for the help
Copy linkTweet thisAlerts:
@NogDogNov 27.2007 — You need to get the values from the $_POST array, e.g.: $_POST['comp_type'] since your form is using the post method. Also, you should filter them through [url=http://www.php.net/mysql_real_escape_string]mysql_real_escape_string()[/url] before using them in a query.
Copy linkTweet thisAlerts:
@crazy8authorNov 27.2007 — Ok so I got this so far. I know its not "correct" im still trying to do it correctly. What do you see that I need to fix? Thanks

Also, wuld it just be better for me to somehow put what I need into a session from each form then insert it into a db? If so how would I do that?
[code=php]<?php

// database information
$host = 'localhost';

$user = 'root';
$password = '';
$dbName = 'hotspot';

// connect and select the database
$conn = mysql_connect($host, $user, $password) or die(mysql_error());
$db = mysql_select_db($dbName, $conn) or die(mysql_error());

// for easier variable handling...
$comp_type = $_POST['comp_type'];
$comp_name = $_POST['comp_name'];
$comp_address = $_POST['comp_address'];
$comp_phone = $_POST['comp_phone'];
$comp_desc = $_POST['comp_desc'];
$comp_email = $_POST['comp_email'];
$comp_web = $_POST['comp_web'];

// insert new entry in the database if entry submitted
if (isset($_POST['comp_type']) && trim($_POST['comp_type']) != '') {
if (isset($_POST['comp_name']) && trim($_POST['comp_name']) != '')
if (isset($_POST['comp_address']) && trim($_POST['comp_address']) != '')
if (isset($_POST['comp_phone']) && trim($_POST['comp_phone']) != '')
if (isset($_POST['comp_desc']) && trim($_POST['comp_desc']) != '')
if (isset($_POST['comp_email']) && trim($_POST['comp_email']) != '')
if (isset($_POST['comp_web']) && trim($_POST['comp_web']) != '')


// insert new entry into database
$sql = "insert into establishment (comp_type,comp_name,comp_address,comp_phone,comp_desc,comp_email,comp_web)
values ('$comp_type','$comp_name','$comp_address','$comp_phone','$comp_desc','$comp_email','$comp_web')";
$result = mysql_query($sql, $conn) or die(mysql_error());
} // end if new entry posted

// select all the entries from the table
$sql = "select * from establishment";
$result = mysql_query($sql, $conn) or die(mysql_error());

// echo out the results to the screen
while ($list = mysql_fetch_array($result)) {
echo "{$list['comp_type']} <br>";
echo "{$list['comp_name']} <br>";
echo "{$list['comp_address']} <br>";
echo "{$list['comp_phone']} <br>";
echo "{$list['comp_desc']} <br>";
echo "{$list['comp_email']} <br>";
echo "{$list['comp_web']} <br>";
} // end while
?> [/code]
×

Success!

Help @crazy8 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...