Why no - new record in database?

Codes here !

Moderators: macek, egami, gesf

kogia
New php-forum User
New php-forum User
Posts: 2
Joined: Sun Jul 29, 2012 12:54 am

Why no - new record in database?

Postby kogia » Sun Jul 29, 2012 1:17 am

I am a beginner within the subject. I am trying to post data from a form into a database table.
I have av webpage with a form:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <link rel="stylesheet" type="text/css" href="style.css" />

   <title>USBgeoA</title>
</head>

<body>
<div id="content">
   <h1>Sammanställning</h1> </br>
   
   <table border="1">
   </br>
   <P><a>STEG 1.</a></P>
   <p>Fyll i alla rutor för att skapa legenden till din presentation</p>
   <tr>
   <td>
   <table>
        <form method="post" action="input.php">
         
         <tr>
            <td><p><a>Dina uppgifter</a></p></td>      
         </tr>
         
         <tr>
            <td><p>Förnamn</p></td>
               <td><input type="text" name="fName" size="25"/>
               </td>
         </tr>
         
         <tr>
            <td><p>Efternamn</p></td>
                <td><input type="text" name="lName" size="25">
                </td>
         </tr>
         
         <tr>
            <td><p>Titel</p></td>
                <td><input type="text" name="titel" size="25"/>
                </td>
         </tr>
         
         <tr>
            <td><p>Avdelning</p></td>
                <td><input type="text" name="avd" size="25"/>
                </td>
         </tr>
         
         <tr>
            <td><p>Myndighet</p></td>
                <td><input type="text" name="mynd" size="25"/>
                </td>
         </tr>
         
         <tr>
            <td><p>Telefon</p></td>
                <td><input type="text" name="tele" size="25"/>
                </td>
         </tr>
         
         <tr>
            <td><p>Email</p></td>
                <td><input type="text" name="email" size="25"/>
                </td>
         </tr>
         
         
         
         <tr>
            <td><p><a>Angående Beställare av sammanställningen:</a></p></td>
               
         </tr>
         
         <tr>
            <td><p>Namn på beställare</p></td>
                <td><input type="text" name="bName" size="25"/>
                </td>
         </tr>
         
         <tr>
            <td><p>Beställares Email</p></td>
                <td><input type="text" name="bEmail" size="25"/>
                </td>
         </tr>
         
         
      <tr>
          <td></td>
          <td align="right"><input type="submit" name="submit" value="Nästa steg"></td>
        </tr>
         
            
   </table>
   </td>
   </tr>
         
</table>
</br>
</div>         
   
</body>
</html>



I have also a PHP file:

Code: Select all

<?php
$con = mysql_connect("localhost","root","myPassword");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$sql="INSERT INTO my_pres (fName, lName, titel, avd, mynd, tele, email, bName, bEmail)
VALUES
('$_POST[fName]','$_POST[fName]','$_POST[titel]','$_POST[avd]','$_POST[mynd]','$_POST[tele]','$_POST[email]','$_POST[bName]','$_POST[bEmail])";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>


I also have setup a database in phpMyAdmin

database name: my_db
table in database: my_pres


Problem I have is that when i push submit button on the HTML form, nothing is submittet to the database and the only thing that happens is that a window open that asks whet to do with the .php file....like if i wanne save or open it.

I am on a Ubuntu 12.04.

User avatar
freshnet
php-forum Active User
php-forum Active User
Posts: 285
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: Why no - new record in database?

Postby freshnet » Sun Jul 29, 2012 3:45 am

There are a couple of bits of bad html in here that could be throwing off your browser

Code: Select all

<h1>Sammanställning</h1> </br>

should be

Code: Select all

<h1>Sammanställning</h1> <br/>


Code: Select all

<table border="1">
   </br>

should be

Code: Select all

<table border="1">
   <br/>


Code: Select all

 <P><a>STEG 1.</a></P>

should be

Code: Select all

 <p><a>STEG 1.</a></p>


You also have a couple of input tags that are not ended:

Code: Select all

<td><input type="text" name="lName" size="25">

should be

Code: Select all

<td><input type="text" name="lName" size="25"/>


I'd suggest fixing all your html and then see if the form works.

kogia
New php-forum User
New php-forum User
Posts: 2
Joined: Sun Jul 29, 2012 12:54 am

Re: Why no - new record in database?

Postby kogia » Sun Jul 29, 2012 4:18 am

The bad html is fixed. Thanx for the hints!

However the problem remains, an "open" "save" file dialog shows up, but no new rows in my database table...:(

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Why no - new record in database?

Postby johnj » Sun Jul 29, 2012 5:59 am

where are you closing the form tag?

User avatar
freshnet
php-forum Active User
php-forum Active User
Posts: 285
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: Why no - new record in database?

Postby freshnet » Sun Jul 29, 2012 11:48 am

yes please post your 'fixed' html and we'll see if it's actually good. I'd suggest running it through an html validator. http://validator.w3.org/

NigelRen
php-forum Active User
php-forum Active User
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am

Re: Why no - new record in database?

Postby NigelRen » Thu Aug 02, 2012 11:34 pm

Do you have LAMP configured on your machine? ( Have a look at http://www.linuxcandy.com/2012/05/how-t ... golin.html if your not sure )
You have to make sure that when using php you are fetching any files/scripts through a web server ( something like http://localhost/script.php ) and not simply selecting the file through something like Nautilus.


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 0 guests