Hello everyone.
I'm quiet new here and trying to get som helpå from you guys.

My main problem is that I have made a form which I want to use to post data to my database.
But it seem to never work.
Firsta problem was; everytime the page loads the form with empty data is automatically sent.
And I don't know what the problem is.

Second;
When i set a primary key on one of the rows in my database table it seems to be fixed (form is not sent on page load)
But then, I can't send by klicking on send form button.
Anyone who know what the problem can be?
Thanks!

Recommended Answers

All 9 Replies

Hi, can you show us your code? Otherwise it not simple to help.

Here is the code I use. But as I said, I am a beginner so I don't what I do wrong here. Thank you.
// Connects to your Database

 mysql_connect("db_host", "db_username", "db_password")
 or die(mysql_error()); 
 mysql_select_db("db_name")
 or die(mysql_error());

 // Insert into database

mysql_query("INSERT INTO festivals (name, logo, country, city, deadline, date_from, place, id, date_to, info, web, about, open_for, flag)
VALUES ('$name', 'logo', 'country', '$city', '$deadline', '$date_from', '$place', '$id', '$date_to', '$info', '$web', '$about', '$open_for', '$flag')");

I have also used various syntaxes within VALUE such as:

VALUES('$_POST[name]')

And so on, but with no luck. I could only post data only once and then it was full stop.

And if you add or die(mysql_error()) to mysql_query() do you receive an error?

mysql_query("INSERT INTO festivals (name, logo, country, city, deadline, date_from, place, id, date_to, info, web, about, open_for, flag) VALUES ('$name', 'logo', 'country', '$city', '$deadline', '$date_from', '$place', '$id', '$date_to', '$info', '$web', '$about', '$open_for', '$flag')") or die(mysql_error());

show us the complete code: html form and php code. Must see how you pass the values from your form to the php code.

If you use post method, first you must take the values from post like this:

$name = $_POST['name'];

where 'name' is the name of the input field from your form.

There are alot of examples on internet to do that.

Here comes the whole form code, I have shortened it out (The country lists have been shortened too) to make the code short and read friendly.
I'm even gonna try $name = $_POST['name']; as advised.
Thank you again.

<?php 
 // Connects to the Database 

 mysql_connect("db_host", "db_username", "db_password") or die(mysql_error()); 
 mysql_select_db("myfestivaldb_co")
 or die(mysql_error());

//Insert into Database 

mysql_query("INSERT INTO festivals (name, logo, country, city, deadline, date_from, place, id, date_to, info, web, about, open_for, flag) VALUES ('$name', '$logo', '$country', '$city', '$deadline', '$date_from', '$place', '$id', '$date_to', '$info', '$web', '$about', '$open_for', '$flag')");
?> 

 <form method="post" action="add.php">
 <!---form-->
<table width="100%" border="0" cellspacing="2" cellpadding="2" id="contact-form">
          <tr>
            <td width="161" colspan="2"><h4>Use this form to add titles.              <br>
              All forms are required.</h4><br></td>
            </tr>
          <tr>
            <td align="right">
            <strong>Festival name:</strong>
            </td>
            <td>
            <div>
            <span class="value">
            <input name="name" type="text" autofocus required id="name" placeholder="Festival Name" tabindex="1">
            </span>
            </div>
            </td>
          </tr>
          <tr>
            <td align="right">
            <strong>Country:</strong></td>
            <td><span class="value">
<select name="country" required id="country" tabindex="2">
<option value="0" label="Select a country ... " selected="selected">Select a country ... </option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Bolivia">Bolivia</option>
</select>
            </span></td>
          </tr>
          <tr>
            <td align="right"><strong>City:</strong></td>
            <td>
            <span class="value">
<input type="text" required id="city" placeholder="Enter festival city" tabindex="3" name="city" ></span>
</td>
          </tr>
          <tr>
            <td align="right"><strong>Language:</strong></td>
            <td>
<input name="lang" type="text" required id="lang" placeholder="Allowed language?" tabindex="4">
</td>
          </tr>
          <tr>
            <td align="right"><strong>Event place:</strong></td>
            <td>
            <input name="place" type="text" required id="place" placeholder="Venue" tabindex="5">
            </td>
          </tr>
          <tr>
            <td align="right" valign="top"><strong>Event dates:</strong></td>
            <td class="date">
            <script type="text/javascript" src="http://www.myfestivaldb.com/js/datepicker.js"></script>
            <strong>From <br>
<input id="date_from" name="date_from" type="text"  tabindex="6" value="">
<br>
To<br><input name="date_to" type="text" tabindex="7" value="">
            </strong>            </td>
          </tr>
          <tr>
            <td align="right"><strong>Applying deadline</strong></td>
            <td>
  <input name="deadline" type="text" id="deadline" tabindex="8" value=""> 
              </td>
          </tr>
          <tr>
            <td align="right" valign="top">
            <strong>Festival  for:</strong>
            </td>
            <td><div>
            <span class="value">
<select name="format" size="4" required onFocus="Enter somthing" id="format" tabindex="9">
<option value="Narrative shorts">Narrative short</option>
<option value="Animated short">Animated short</option>
<option value="Animated feature">Animated feature</option>
<option value="Documentary">Documentary</option>
<option value="Narrative feature">Narrative feature</option>
<option value="Open for all formats" selected>Open for all formats</option>
            </select>
            </span>
            </div>
            </td>
          </tr>
          <tr>
            <td align="right" valign="top"><strong>Webpage:</strong></td>
            <td>
              <input name="web" type="text" required id="web" placeholder="Begin with http://" tabindex="11" value="">
              </td>
          </tr>
          <tr>
            <td align="right" valign="top"><strong>Info page:</strong></td>
            <td>
            <input name="infopage" type="text" required id="infopage" placeholder="Begin with. http://" tabindex="12" value="">
            </td>
          </tr>
          <tr>
            <td align="right" valign="top"><strong>Logo image adress:</strong></td>
            <td>
            <input name="logo" type="text" required id="logo" placeholder="Begin with. http://" tabindex="13" value="">
            </td>
          </tr>
          <tr>
            <td align="right" valign="top"><strong>Flag image adress:</strong></td>
            <td>
            <input name="flag" type="text" required id="flag" placeholder="Begin with. http://" tabindex="14" value="">
            </td>
          </tr>
          <tr>
            <td align="right" valign="top"><strong>Festival open for:</strong></td>
            <td>
            <span class="value">
<select name="open_for" required id="open_for" tabindex="15">
<option value="0" selected="selected">All countries</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
                <option value="Algeria">Algeria</option>
                <option value="American Samoa">American Samoa</option>
                <option value="Argentina">Argentina</option>
                <option value="Armenia">Armenia</option>
                <option value="Aruba">Aruba</option>
                <option value="Vanuatu" label="Vanuatu">Vanuatu</option>
                <option value="Vatican City" label="Vatican City">Vatican City</option>
              </select>
            </span></td>
          </tr>
          <tr>
            <td align="right" valign="top">About the festival</td>
            <td>
            <textarea name="about" required id="about" placeholder="About the festival" tabindex="16">

Alright, these are thw new changes I made in the code and as you may see the code is totally reconstructed.
But the problem now is. I get the error message: 'Unable to select table.'??

<?php
//initilize PHP
if ( isset ( $_POST['submit'] ) ) //If submit is hit
{
//then connect as user
//change username and password to your mySQL username and password
mysql_connect("db_host", "db_username", "db_password"); 
 mysql_select_db("db_name") or die(mysql_error()); 
//convert all the posts to variables:
$name = $_POST['name'];
$logo = $_POST['logo'];
$country = $_POST['country'];
$city = $_POST['city'];
$deadline = $_POST['deadline'];
$date_from = $_POST['date_from'];
$venue = $_POST['venue'];
$date_to = $_POST['date_to'];
$info_page = $_POST['info_page'];
$web = $_POST['web'];
$about = $_POST['about'];
$open_for = $_POST['open_for'];
$flag = $_POST['flag'];

//Insert the values into the correct database with the right fields.

$result = mysql_query("INSERT INTO festivals (name, logo, country, city, deadline, date_from, venue, date_to, info_page, web, about, open_for, flag)".
"VALUES('NULL','$name', '$logo', '$country', '$city', '$deadline', '$date_from', '$venue',, '$date_to', '$info_page', '$web', '$about', '$open_for', '$flag')")
or die( "<p><span style=\"color: red;\">Unable to select table</span></p>");
mysql_close();
//confirm
echo "<p><span style=\"color: red;\">Thank You; your comments have been entered in my database. DO NOT REFRESH THE PAGE or data will be sent again.</span></p>";
}
else
{
// close php so we can put in our code
?>
 <form method="post" action="add.php">
 <!---form-->

But the problem now is. I get the error message: 'Unable to select table.'?

You get that because of:

or die( "<p><span style=\"color: red;\">Unable to select table</span></p>");

To get the real error message use mysql_error() after the query:

$result = mysql_query("INSERT INTO festivals (name, logo, country, city, deadline, date_from, venue, date_to, info_page, web, about, open_for, flag)". "VALUES('NULL','$name', '$logo', '$country', '$city', '$deadline', '$date_from', '$venue',, '$date_to', '$info_page', '$web', '$about', '$open_for', '$flag')") or die(mysql_error());

Thank you very much everyone.
It does work now as it should.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.