13 Replies - 3029 Views - Last Post: 28 January 2011 - 05:49 AM Rate Topic: -----

#1 dannymitza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-October 10

PHP insert into mysql database

Posted 26 January 2011 - 03:18 PM

Hello. I need help with inserting data from a HTML form into a database using PhP, without opening other page when submit.
I have the basic PhP code (insert.php):
<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);
mysql_query("INSERT INTO table (value1, value2, value3)
VALUES ('x', 'y', 'z')");
mysql_close($con);
?> 



And the HTML form is:
<html>
<body>

<form action="insert.php" method="post">
Row 1: <input type="text" name="value1" />
Row 2: <input type="text" name="value2" />
Row 3: <input type="text" name="value3" />
<input type="submit" />
</form>

</body>
</html> 



So, what I want to make is when I click on submit to display a message (succesful or error) in the same page with form.

If someone knows how to do it, I'll be thankful.

Is This A Good Question/Topic? 0
  • +

Replies To: PHP insert into mysql database

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: PHP insert into mysql database

Posted 26 January 2011 - 03:31 PM

PHP is a server-side language so in order to mess with the server from the client without a page restart would mean AJAX would it not?
Was This Post Helpful? 0
  • +
  • -

#3 dannymitza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-October 10

Re: PHP insert into mysql database

Posted 26 January 2011 - 03:35 PM

Well, I dont know in what language I must do that, I was thinking it was about PhP :(...
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: PHP insert into mysql database

Posted 26 January 2011 - 03:40 PM

I don't know much about AJAX, but it uses PHP and Javascript from what I understand. Here's a tutorial that covers it: http://www.dreaminco...t-for-asp-ajax/
Was This Post Helpful? 0
  • +
  • -

#5 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: PHP insert into mysql database

Posted 26 January 2011 - 07:22 PM

You can take advantage of a javascript library like jQuery to simplify the task for you. jQuery has several methods for simplifying ajax requests. Its really useful library and very easy to learn jQuery. jQuery documentation page contains useful resources and this page lists available methods for handling ajax requests.
Was This Post Helpful? 1
  • +
  • -

#6 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: PHP insert into mysql database

Posted 26 January 2011 - 07:44 PM

Or you could simplify everything and use iframe. You will never leave the page, the iframe will just change the the action page.
Was This Post Helpful? 0
  • +
  • -

#7 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: PHP insert into mysql database

Posted 26 January 2011 - 08:29 PM

Inline frames are quickly becoming less desirable. AJAX is absolutely the way to go, and atik97 has laid out a great plan for you in terms of getting started. You'll also find jQuery extremely useful later on down the road, so it's a great library to learn now and use for years to come.
Was This Post Helpful? 1
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,466
  • Joined: 27-December 08

Re: PHP insert into mysql database

Posted 26 January 2011 - 10:53 PM

I have to agree with Valek. One thing my boss at my internship commented on my nonprofit site was that the iframes we used didn't work well on his iPad.
Was This Post Helpful? 0
  • +
  • -

#9 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: PHP insert into mysql database

Posted 26 January 2011 - 10:58 PM

Not to mention that when put up against each other, AJAX is smoother overall than inline frames in virtually every application I've seen.
Was This Post Helpful? 1
  • +
  • -

#10 dannymitza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 23-October 10

Re: PHP insert into mysql database

Posted 28 January 2011 - 04:59 AM

Well thank you guys! I'm starting learn AJAX and jQuery :)
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: PHP insert into mysql database

Posted 28 January 2011 - 05:25 AM

View PostDogstopper, on 26 January 2011 - 11:40 PM, said:

I don't know much about AJAX, but it uses PHP and Javascript from what I understand.

PHP is not a requirement*. you can use any server-side language, as long as there is a HTTP interface of some kind (so for instance Java and Perl would work equally well)



* - though PHP is probably the most common when thinking of web applications.
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: PHP insert into mysql database

Posted 28 January 2011 - 05:30 AM

AJAX = Asynchronous JavaScript And XML. Although XML is no longer the only player in the data, JSON (JavaScript Object Notation) is a very big part now.

As Dormilich stated, PHP is definitely not a requirement.
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: PHP insert into mysql database

Posted 28 January 2011 - 05:32 AM

JSON doesnt eat as much resources as XML does.
Was This Post Helpful? 1
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: PHP insert into mysql database

Posted 28 January 2011 - 05:49 AM

As someone who deals with XML constantly (we went quite overboard with our XML in our software), I couldn't agree more!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1