/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Problem with a form

Here’ my form:

[code=html]
<form action=”./Pages/Homepage/submit.php” method=”post” id=”newsite”>
<fieldset>
<legend>NEW SITE</legend>
<ul>
<li><div><label>Site Title</label><input type=”text” class=”text” name=”site_title”></div></li>
<li><div><label>URL</label><input type=”text” class=”text” name=”url”></div></li>
<li><div><label>Keywords</label><input type=”text” class=”text” name=”keywords”></div></li>
<li><div><label>Mature</label><input type=”checkbox” class=”checkbox” name=”Mature”></div></li>
<li class=”center”><div><input type=”submit” value=”Submit” class=”submit”></div></li>
</ul>
</fieldset>
</form>
[/code]

Here’s the PHP to go with it:

[code=php]
<?php
$mysql = array(
‘hostname’ => ‘localhost’,
‘username’ => ‘root’,
‘password’ => ”,
‘database_name’ => ‘Lynx’,
‘table_name’ => ‘links’
);
//Database Connection
if (!@mysql_connect($mysql[‘hostname’], $mysql[‘username’], $mysql[‘password’]))
{
exit(‘<strong>Connection mistake. The database is borked: </strong> <br> <pre>’ . mysql_errno() . ‘ / ‘ . mysql_error());
}

//Database selection
if (!@mysql_select_db($mysql[‘database_name’]))
{
exit(‘<strong>Database went a-borking: </strong> <br> <pre>’ . mysql_errno() . ‘ / ‘ . mysql_error());
}

$form = array(
‘Title’,
‘URL’,
‘Keywords’,
‘Mature’
);

$max_form_len = 255;
$error=”;
//Loop through the post variable data
foreach($_POST as $key => $value)
{
//if this variable is invalid, dump it
if(!array_key_exists($key, $form))
{
continue;
}

//If this variable is empty, squawk
if (empty($_POST[$key]))
{
$error .= ‘You must enter a ‘ . ucwords(str_replace(‘_’, ‘ ‘, $key)) . ‘.<br>’;
}

//If variable exceeds maximum length.
if (strlen($_POST[$key])>$max_form_len)
{
$error .= ‘Your ‘ . ucwords(str_replace(‘_’, ‘ ‘, $key)) . ‘ exceeds the maximum allowed length.’;
$error .= ‘Please limit your ‘ . ucwords(str_replace(‘_’, ‘ ‘, $key)) . ‘ to ‘ . $max_form_len. ‘ characters (current length: ‘ . strlen($_POST[$key]) . ‘).<br>’;
continue;
}
}
if (!empty($error))
{
exit($error);
}

$result = @mysql_query(<<<sql
INSERT INTO `{$mysql[‘table_name’]}` (Title, URL, Keywords, Mature)
VALUES(‘$rating’, ‘$site_title’, ‘$url’, ‘$keywords’)
sql
);

if(!$result)
{
exit(‘<strong>An unknown MySQL error has occurred: </strong><br /><pre>’ . mysql_errno() . ‘ / ‘ . mysql_error() . “nQuery: $result”);
}

exit(‘Site successfully added.’);
?>
[/code]

When ever I use that form, I get a blank row in the mySQL database. Where am I going wrong?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@tarsusOct 16.2006 — For starters, this array is setting values, not keys:

[code=php]$form = array(
'Title',
'URL',
'Keywords',
'Mature'
);[/code]


So you end up with:

[code=php]$form[0]='Title';[/code]

Not:

[code=php]$form['Title'][/code]

And the values in this array don't match the names of your form inputs, anyway. Your form has an input named 'site_title', not 'Title' - and 'url', not 'URL'. So your loop checking for errors is never going to catch any.

Then, in your query, you are trying to use variables $rating, $site_title, $url, and $keywords, which you have never defined. You have $_POST['site_title'] available, but you have never defined any $site_title.
Copy linkTweet thisAlerts:
@pcthugOct 17.2006 — Try this, it fixes all of the mentioned errors:
[code=php]<?php
$mysql = array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database_name' => 'Lynx',
'table_name' => 'links'
);
//Database Connection
if (!@mysql_connect($mysql['hostname'], $mysql['username'], $mysql['password']))
{
exit('<strong>Connection mistake. The database is borked: </strong> <br> <pre>' . mysql_errno() . ' / ' . mysql_error());
}

//Database selection
if (!@mysql_select_db($mysql['database_name']))
{
exit('<strong>Database went a-borking: </strong> <br> <pre>' . mysql_errno() . ' / ' . mysql_error());
}


$form = array(
'title',
'url',
'keywords',
'Mature'
);

$max_form_len = 255;
$error='';
//Loop through the post variable data
foreach($_POST as $key => $value)
{
//if this variable is invalid, dump it
if(!in_array($key, $form))
{
continue;
}

//If this variable is empty, squawk
if (empty($_POST[$key]))
{
$error .= 'You must enter a ' . ucwords(str_replace('_', ' ', $key)) . '.<br>';
}

//If variable exceeds maximum length.
if (strlen($_POST[$key])>$max_form_len)
{
$error .= 'Your ' . ucwords(str_replace('_', ' ', $key)) . ' exceeds the maximum allowed length.';
$error .= 'Please limit your ' . ucwords(str_replace('_', ' ', $key)) . ' to ' . $max_form_len. ' characters (current length: ' . strlen($_POST[$key]) . ').<br>';
continue;
}

$$key = @mysql_real_escape_string($_POST[$key]);
}
if (!empty($error))
{
exit($error);
}

$result = @mysql_query(<<<sql
INSERT INTO {$mysql['table_name']} (Title, URL, Keywords, Mature)
VALUES('$site_title', '$url', '$keywords', '$Mature')
sql
);

if(!$result)
{
exit('<strong>An unknown MySQL error has occurred: </strong><br /><pre>' . mysql_errno() . ' / ' . mysql_error() . "nQuery: $result");
}

exit('Site successfully added.');
?> [/code]
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorOct 17.2006 — For some reason, it's not reading my "Mature" checkbox.
Copy linkTweet thisAlerts:
@pcthugOct 17.2006 — What format (true|false, 1|0, etc.) would you like the result to be stored in the database?
Copy linkTweet thisAlerts:
@pcthugOct 18.2006 — Try this script then, it sets $Mature to 1 if the checbox is checked and 0 if not:[code=php]<?php
$mysql = array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database_name' => 'Lynx',
'table_name' => 'links'
);
//Database Connection
if (!@mysql_connect($mysql['hostname'], $mysql['username'], $mysql['password']))
{
exit('<strong>Connection mistake. The database is borked: </strong> <br> <pre>' . mysql_errno() . ' / ' . mysql_error());
}

//Database selection
if (!@mysql_select_db($mysql['database_name']))
{
exit('<strong>Database went a-borking: </strong> <br> <pre>' . mysql_errno() . ' / ' . mysql_error());
}


$form = array(
'title',
'url',
'keywords'
);

$max_form_len = 255;
$error='';
//Loop through the post variable data
foreach($_POST as $key => $value)
{
//if this variable is invalid, dump it
if(!in_array($key, $form))
{
continue;
}

//If this variable is empty, squawk
if (empty($_POST[$key]))
{
$error .= 'You must enter a ' . ucwords(str_replace('_', ' ', $key)) . '.<br>';
}

//If variable exceeds maximum length.
if (strlen($_POST[$key])>$max_form_len)
{
$error .= 'Your ' . ucwords(str_replace('_', ' ', $key)) . ' exceeds the maximum allowed length.';
$error .= 'Please limit your ' . ucwords(str_replace('_', ' ', $key)) . ' to ' . $max_form_len. ' characters (current length: ' . strlen($_POST[$key]) . ').<br>';
continue;
}

$$key = @mysql_real_escape_string($_POST[$key]);
}

// set Mature variable int boolean
$Mature = (isset($_POST['Mature'])) ? 1 : 0;

if (!empty($error))
{
exit($error);
}

$result = @mysql_query(<<<sql
INSERT INTO {$mysql['table_name']} (Title, URL, Keywords, Mature)
VALUES('$site_title', '$url', '$keywords', '$Mature')
sql
);

if(!$result)
{
exit('<strong>An unknown MySQL error has occurred: </strong><br /><pre>' . mysql_errno() . ' / ' . mysql_error() . "nQuery: $result");
}

exit('Site successfully added.');
?> [/code]
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorOct 18.2006 — Thanks, it's working perfectly now. I have more issues, but they're on another topic, so I'll ask them on a different thread
×

Success!

Help @Mr_Initial_Man 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.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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