Free Web Hosting Forum
Go Back   Free Web Hosting Forum > Website Building > Web Programming
Reload this Page Insert data into mysql database
Reply
 
Thread Tools Display Modes
(#1 (permalink))
Old
Junior Member
Silva is on a distinguished road
 
Posts: 6
Join Date: Sep 2011
Question Insert data into mysql database - 09-12-2011, 01:17 PM

Dear,
Helllo, I find problems to insert into mysql database with the remote server however, when I work on Localhost everything is going on well, but time to have hosted on the mysql websever it fails to inssert and I notice blank fields that are supposed to have data, please find the form code below that I use to work on Localhost and tell if exists any error.

The form codes

<form method="post" action="Message.php">
<center>
<table width="467" height="196" cellpadding="0">
<tr>
<td width="314"><div align="right" class="Style11"><font color="red">*</font> Nom : </div></td>
<td width="347"><div align="left">
<input type="text" name="nom" size="30" value="" maxlength="35" />
</div></td>
</tr>
<tr>
<td width="314"><div align="right" class="Style11"><font color="red">*</font> Post-nom : </div></td>
<td width="347"><div align="left">
<input type="text" name="post_nom" size="30" value="" maxlength="35" />
</div></td>
</tr>
<tr>
<td><div align="right" class="Style11"><font color="red">*</font> E-mail :</div></td>
<td><div align="left">
<input type="text" name="mail" size="30" value="" maxlength="35" />
</div></td>
</tr>
<tr>
<td><div align="right" class="Style11"><font color="red">*</font>Message :</div></td>
<td><div align="left">
<textarea name="message" rows="8" cols="45"></textarea>
</div></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><div align="left">
<p>
<input type="submit" value="Envoyer" name="Envoyer"/>
<input name="R&eacute;initialiser" type="reset" value="R&eacute;initialiser"/>
</p>
</div></td>
</tr>
</table>
<p>&nbsp;</p>
</center>
<span class="Style11">Les champs marqu&eacute;s par <font color="red">*</font> sont obligatoires </span>
</form>

the php scripts

<?php

//On recupere les champs du formulaire forum
if(isset($_POST['nom'])) $nom=$_POST['nom'];
else $nom="";

if(isset($_POST['post_nom'])) $post_nom=$_POST['post_nom'];
else $post_nom ="";

if(isset($_POST['mail'])) $mail=$_POST['mail'];
else $mail ="";

if(isset($_POST['message'])) $message=$_POST['message'];
else $message="";


$amgo = mysql_pconnect('mysql12.000webhost.com','mydb_name ','pw') or die ('Erreur de connexion'.mysql_error());

mysql_select_db('a7611110_amgo',$amgo) or die ('Erreur de selection'.mysql_error());

$sql = "INSERT INTO suggestions (id, nom, post_nom, mail, message) VALUES('','$nom','$post_nom','$mail','$message')";

mysql_query($sql) or die('ErreurSQL !'.$sql.'<br>'.mysql_error());


mysql_close($amgo);
?>
Reply With Quote
Sponsored Links
(#2 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 544
Join Date: Dec 2010
Default 09-12-2011, 05:12 PM

For "if ~ else " syntax, please add braces {} as shown below:

if(isset($_POST['nom'])) {$nom=$_POST['nom'];}
else {$nom="";}

if(isset($_POST['post_nom'])) {$post_nom=$_POST['post_nom'];}
else {$post_nom ="";}

if(isset($_POST['mail'])) {$mail=$_POST['mail'];}
else {$mail ="";}

if(isset($_POST['message'])) {$message=$_POST['message'];}
else {$message="";}
Reply With Quote
(#3 (permalink))
Old
d3iti's Avatar
Super Moderator
d3iti is on a distinguished road
 
Posts: 4,609
Join Date: Jul 2009
Location: Spain
Default 09-12-2011, 05:22 PM

HTML code have not changes.
HTML Code:
 <form method="post" action="Message.php">
      <center>
        <table width="467" height="196" cellpadding="0">
          <tr>
            <td width="314"><div align="right" class="Style11"><font color="red">*</font> Nom : </div></td>
            <td width="347"><div align="left">
                <input type="text" name="nom" size="30"  value="" maxlength="35" />
            </div></td>
          </tr>
          <tr>
            <td width="314"><div align="right" class="Style11"><font color="red">*</font> Post-nom : </div></td>
            <td width="347"><div align="left">
                <input type="text" name="post_nom" size="30"  value="" maxlength="35" />
            </div></td>
          </tr>
          <tr>
            <td><div align="right" class="Style11"><font color="red">*</font> E-mail :</div></td>
            <td><div align="left">
                <input type="text" name="mail" size="30"  value="" maxlength="35" />
            </div></td>
          </tr>
          <tr>
            <td><div align="right" class="Style11"><font color="red">*</font>Message :</div></td>
            <td><div align="left">
                <textarea name="message" rows="8" cols="45"></textarea>
            </div></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><div align="left">
                <p>
                  <input type="submit" value="Envoyer" name="Envoyer"/>
                  <input name="R&eacute;initialiser" type="reset" value="R&eacute;initialiser"/>
                </p>
            </div></td>
          </tr>
        </table>
        <p>&nbsp;</p>
      </center>
      <span class="Style11">Les champs marqu&eacute;s par <font color="red">*</font> sont obligatoires </span>
    </form>
the php scripts . File Message.php (The server is case sensitive. You should respect upper and lower case in file name.)
PHP Code:
<?php 

//On recupere les champs du formulaire forum
if(isset($_POST['nom'])) $nom=$_POST['nom'];
    else    
$nom="";

if(isset(
$_POST['post_nom'])) $post_nom=$_POST['post_nom'];
    else    
$post_nom ="";
    
if(isset(
$_POST['mail'])) $mail=$_POST['mail'];
    else    
$mail ="";
    
if(isset(
$_POST['message'])) $message=$_POST['message'];
    else    
$message="";
    

$amgo mysql_pconnect('mysql12.000webhost.com','mydb_name','pw') or die ('Erreur de connexion'.mysql_error());

mysql_select_db('a7611110_amgo',$amgo) or die ('Erreur de selection'.mysql_error());

$sql "INSERT INTO suggestions (id, nom, post_nom, mail, message) VALUES('','$nom','$post_nom','$mail','$message')";
/* ADD NEXT LINE ********************/
echo"<p>The SQL query is: ".$sql;
/*******************************************/

mysql_query($sql) or die('ErreurSQL !'.$sql.'<br>'.mysql_error());
    

mysql_close($amgo);
?>


Si este mensaje te ha servido de ayuda no dudes en pulsar sobre el botón karma

Last edited by d3iti; 09-12-2011 at 05:24 PM.
Reply With Quote
(#4 (permalink))
Old
Leder678's Avatar
Senior Member
Leder678 is on a distinguished road
 
Posts: 1,430
Join Date: Jan 2009
Location: Norway
Send a message via MSN to Leder678
Default 09-12-2011, 06:21 PM

Quote:
Originally Posted by grace1004 View Post
For "if ~ else " syntax, please add braces {} as shown below:

if(isset($_POST['nom'])) {$nom=$_POST['nom'];}
else {$nom="";}

if(isset($_POST['post_nom'])) {$post_nom=$_POST['post_nom'];}
else {$post_nom ="";}

if(isset($_POST['mail'])) {$mail=$_POST['mail'];}
else {$mail ="";}

if(isset($_POST['message'])) {$message=$_POST['message'];}
else {$message="";}

When there is one line of code after an if/else statement, the baces aren't needed, e.g.:
PHP Code:
if($this == $that)
   echo 
"This is that!";
else
   echo 
"This is not that... :("
One line is till the next semicolon, in other words, you may call functions after each "if/else"


Follow me on twitter @Mortenrb

W3Fools - Read and learn

Please AT LEAST read the 10 bolded lines of the TOS at:
http://www.000webhost.com/includes/tos.php
Reply With Quote
(#5 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 544
Join Date: Dec 2010
Default 09-13-2011, 02:02 AM

I tested your code after making a table in my database in free hosting, and it worked fine.

PHP Code:
<?
if(isset($_POST['nom'])) $nom=$_POST['nom'];
else 
$nom="";

if(isset(
$_POST['post_nom'])) $post_nom=$_POST['post_nom'];
else 
$post_nom ="";

if(isset(
$_POST['mail'])) $mail=$_POST['mail'];
else 
$mail ="";

if(isset(
$_POST['message'])) $message=$_POST['message'];
else 
$message="";

$conn mysql_connect("mysqlxx.000webhost.com""a3367xxx_test","******") or die(mysql_error());
mysql_select_db("a3367xxx_test"$conn);

$sql "INSERT INTO if_else (id, nom, post_nom, mail, message) VALUES('', '$nom','$post_nom','$mail','$message')";

mysql_query($sql) or die('ErreurSQL !'.$sql.'<br>'.mysql_error());

mysql_close($conn);
?>
Please check your database connection script once more, as my code is same as yours except database
connection script. Please note that mysql_user name and mysql_db name are same in my DB connection
script. Is it the same in your case?

Last edited by grace1004; 09-13-2011 at 08:42 AM.
Reply With Quote
(#6 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 544
Join Date: Dec 2010
Default 09-13-2011, 08:43 AM

Leder678, with one line if ~ else statement, I think both cases (with braces or without braces) are
commonly used. I found the following example from http://kr.php.net/manual/en/control-structures.else.php:

PHP Code:
<?php
if ($a $b) {
  echo 
"a is greater than b";
} else {
  echo 
"a is NOT greater than b";
}
?>

Last edited by grace1004; 09-13-2011 at 08:55 AM.
Reply With Quote
(#7 (permalink))
Old
Leder678's Avatar
Senior Member
Leder678 is on a distinguished road
 
Posts: 1,430
Join Date: Jan 2009
Location: Norway
Send a message via MSN to Leder678
Default 09-13-2011, 03:12 PM

Quote:
Originally Posted by grace1004 View Post
Leder678, with one line if ~ else statement, I think both cases (with braces or without braces) are
commonly used. I found the following example from http://kr.php.net/manual/en/control-structures.else.php:

PHP Code:
<?php
if ($a $b) {
  echo 
"a is greater than b";
} else {
  echo 
"a is NOT greater than b";
}
?>
The persons whom are new (or haven't learned the a bit more advanced rules) with php will normally use the brace structure (which i find pretty good, because in some cases the script looks better)

For instance, I'd use
PHP Code:
if($a $b)
   
func();
else
   
func2(); 
If I had multiple (like "if"&"elseif"&"else") statements, but if I had a single if/else statement, I'd define the $var instantly using:

PHP Code:
$var = ($a b) ? func() : func2(); 


Follow me on twitter @Mortenrb

W3Fools - Read and learn

Please AT LEAST read the 10 bolded lines of the TOS at:
http://www.000webhost.com/includes/tos.php
Reply With Quote
(#8 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 544
Join Date: Dec 2010
Default 09-14-2011, 05:36 AM

OK, everybody can use whatever structure they prefer (with braces or without braces, even ternary operator you showed).
I prefer the brace structure shown in the above example of php.net. This means that I am a beginner of php learning

By the way, do you have any other suggestion on Silva's question?

Last edited by grace1004; 09-14-2011 at 06:20 AM.
Reply With Quote
(#9 (permalink))
Old
Junior Member
Silva is on a distinguished road
 
Posts: 6
Join Date: Sep 2011
Default 09-14-2011, 11:00 AM

Yes!!! Thank you Grace1004 and d3iti, Braces are helping to avoid blank entry in the database otherwise the data cannot be inserted so far, @ Grace1004 I tried your code as you said to work fine within the msql12.000webhost.com and it isn't yet going well, I think to create another table to try,Soon.
Reply With Quote
(#10 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 544
Join Date: Dec 2010
Default 09-14-2011, 11:21 AM

For your reference, I created my table with the following code:

create table if_else (
id int(11) not null auto_increment,
nom varchar(20),
post_nom varchar(20),
mail varchar(50),
message text,
primary key (id)
);
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com