PHP Form to add a data in SQL

Status
Not open for further replies.

azamkandy

New Member
Messages
32
Reaction score
0
Points
0
Hi ,
I just created a database and trying to add some records from a php form but firstly when i set the database name in to x10 server name ex:
mysql_connect(mysql-starka.x10hosting.com ,$username,$password);

It says access denied and then I changed the database name in to localhost ex: the url changes in to .php files url page is blank i can see DONE in the bottom of the page i mean the page loading status but the record is not in the database, cant add any records .
check this url : http://www.majaradio.com/majaradio.com/from.html

The php ::


<?
$username='azamkand_abc';
$password='abcdef';
$database='azamkand_dedicates';

$name=$_POST['first'];
$email=$_POST['email'];
$song=$_POST['song'];
$ded_for=$_POST['ded_for'];
$comm=$_POST['comm'];


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable ");

$query = "INSERT INTO dedications VALUES ('','$name','$email','$song','$ded_for','$comm')";
mysql_query($query);

mysql_close();
?>
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
Try replacing mysql_query($query) with this:
Code:
$result = mysql_query($query);

if ($result === false) {
  die(mysql_error());
}

Make sure you don't use this code on a production website! It could give away information and is a security risk!
 

azamkandy

New Member
Messages
32
Reaction score
0
Points
0
Beautiful What is the magic???? Sorry I'm not too good with php and sql
And also if I want to add a url after the adding the record what should I do???
 
Last edited:

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
PHP:
<?php
$username='azamkand_abc';
$password='abcdef';
$database='azamkand_dedicates';

$name = mysql_real_escape_string($_POST['first']);
$email = mysql_real_escape_string($_POST['email']);
$song = mysql_real_escape_string($_POST['song']);
$ded_for = mysql_real_escape_string($_POST['ded_for']);
$comm = mysql_real_escape_string($_POST['comm']);


mysql_connect(localhost,$username,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

$query = 'INSERT INTO dedications VALUES ("","$name","$email","$song","$ded_for","$comm")';
mysql_query($query) or die(mysql_error());

mysql_close();
?>

try this
 
Last edited:

azamkandy

New Member
Messages
32
Reaction score
0
Points
0
Thanks Alot guys!!

And I just wondering how to add a page after a user submits a form it should redirect to that page like thank you page or home page...;)
 

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
Thanks Alot guys!!

And I just wondering how to add a page after a user submits a form it should redirect to that page like thank you page or home page...;)

try adding:

PHP:
header("Location: path/to/file");
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
Ah, Viggie_swe, you're too quick!

That code will work very well, but whenever you see "or die(mysql_error())" make sure you either remove it completely or use some other error message so that sensitive information is not given away to normal users when your site is in production.

A simple
Code:
or die('Database Error');
Will work and won't give away anything important :)
 
Last edited:

azamkandy

New Member
Messages
32
Reaction score
0
Points
0
Thanks Alot Viggie_s & garrettroyce


It's
Perfect now!!!
;)


 
Last edited:
Status
Not open for further replies.
Top