Problem with PHP and Mysql integration


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with PHP and Mysql integration
# 1  
Old 10-31-2011
Problem with PHP and Mysql integration

Hi Experts,

I am a beginner of PHP and trying to create a tutorial database. My web interface takes a value and inserts into table very correctly. But when I refresh the same interface, it inserts the same value again and again. My code is below:

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

// Create database
if (mysql_query("CREATE DATABASE naushad1",$con))
  {
  echo 
"Database created";
  }
/*else
  {
  echo "Error creating database: " . mysql_error();
  }*/

// Create table
mysql_select_db("naushad1"$con);
$sql "CREATE TABLE Persons
(
FirstName varchar(15) primary key,
LastName varchar(15),
Age int
)"
;

// Execute query
mysql_query($sql,$con);

mysql_close($con);
$con mysql_connect("localhost","root","India123");
if (!
$con)
  {
  die(
'Could not connect: ' mysql_error());
  }

mysql_select_db("naushad1"$con);

$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('
$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

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

mysql_close($con);
Please help.

Thanks,
Deepak
# 2  
Old 10-31-2011
You will find better help in a MySQL forum.
# 3  
Old 10-31-2011
Of course it does, but it's not an error of MySQL or PHP. When you refresh the page, the browser just sends the same information again, and your table accepts duplicate values. All of this is accepted behaviour, and if you want it different you'll have to change your table to only accept unique values (eg. by using an INDEX), or do the checks with PHP.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Web Development

Problem in printing binary tree using php and mysql

Database Structure Root Table ID Root_ Node Level 1 A 0 2 B 1 3 C 1 Child Table ID Left_Node Right_Node Root_Node Root_ID 1 B C A 1 ... (1 Reply)
Discussion started by: Deepak Tiwari
1 Replies

2. Programming

PHP and MySQL

Hello, While I was interpretation the PHP manual on database security the recent past, it said that you should by no means connect to the database as the super user but rather as one more user with more limited options. My question is: How do you generate new users and set access... (2 Replies)
Discussion started by: AimyThomas
2 Replies

3. Emergency UNIX and Linux Support

Migration of website... PHP/Mysql -which path for DB.php

Hi, I have two websites: website1.com and website2.com I didn't write either but have successfully moved all the files from website1.com to website2.com I (thought) I installed all the correct php modules and website2 is mostly up and running. However, my boss found that when we go to a... (15 Replies)
Discussion started by: Astrocloud
15 Replies

4. Programming

MySQL - PHP

Hello every one i have question i want to build DATAbase using PHP as interface i use shell to access to linux . i have in linux psql and SQLplus i'll call all html files that has db tabels from shell directory. what should to do before design php pages. can build the database sql design... (3 Replies)
Discussion started by: Scotch
3 Replies

5. Programming

Linux and Php integration help .

I m devoloping a website which communicate with Linux server . Whenever a page is loaded it runs one script on Linux server. My problem is a simple commands like date , ls runs very smoothly. But whenever i execute big script.It wont display the output. My knowledge of PHP is very less i m... (2 Replies)
Discussion started by: pinga123
2 Replies

6. Shell Programming and Scripting

Mysql problem

No help guys??? :( Hi guys, i've got a question here... my intention is to write a script that would access a mysql db and extract some info and paste it into a file... so the first move would be, to access the mysql interface right? hence... #! /usr/bin/ksh echo "Job Begin: " `date` ... (1 Reply)
Discussion started by: 12yearold
1 Replies

7. Shell Programming and Scripting

Problem with PHP and MySQL

Okay, I'm new to this PHP and MySQL stuff, so help would be VERY much appreciated. :) On my iMac runnning Panther, it has MySQL and PHP installed. Yet when I view a PHP file from the iMac or another computer at my house, I get the source code. What's wrong? (11 Replies)
Discussion started by: Danny_10
11 Replies

8. UNIX for Advanced & Expert Users

MySQL problem >> missing mysql.sock

MySQL on my server is down.... I figured out that the mysqld process isn't running. When I try to run it, it says it can't find mysql.sock Any suggestions? Here's what I can't do: can't be root don't have physical access (do stuff via SSH) reinstall MySQL (need to keep the current MySQL... (8 Replies)
Discussion started by: _hp_
8 Replies
Login or Register to Ask a Question