Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Inserting a record via a hyperlink into a mySQL database

Posted on 2009-04-14
5
Medium Priority
?
301 Views
Last Modified: 2012-05-06
I've spent the day playing with MySQL and linking to the database via a local ODBC connection - all seems good.  I will be sending out a hyperlink (something like); http://www.xxxx.com/mypage.php?TSID=123456

I now have a test ID database, configured in the following way:

Database name: test1
Username: user
Password: password
Table name: TS1
Field name: Field1

Could someone give me a really basic example of the html page and the PHP page containing the code (I'm really new to this).  Ultimately, I need to know how to get the '123456' into Field1 (as a new record).

Thanks as always.
0
Comment
Question by:Andy Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
Undisputed earned 2000 total points
ID: 24143348
Here's a quick & simple code that you can use.  I did not include any data validation but noted where you can add them if you wanted to.  The validation would occur server side but you can easily add javascript to do client side validation which is what I would suggest.  But that is beyond the scope of this question, so I'll leave that alone :)
<?php
//connect to db
$dbh = new PDO('mysql:host=localhost;dbname=test1', user, password, array( PDO::ATTR_PERSISTENT => true ));
 
if(empty($_GET['TSID']) == FALSE)
{
    $tsid = trim($_GET['TSID']);
    $return_message = 'TSID: ' . $tsid;
 
    //Do some validations here is desired
 
    //Insert into db
    $sql = 'INSERT INTO TS1 (`Field1`) VALUES (:tsid)';
    $rslt = $dbh->prepare($sql);
 
    if($rslt->exec(array('tsid' => $tsid)) or die(print_r($rslt->errorInfo())))
    {
       $return_message .= ' has been inserted into db.';
    }
    else
    {
       $return_message .= ' was not inserted into db.';
    }
 
}
 
//Output html
print <<<HTML_CODE
<html>
<body>
<div id="return_message">
$return_message
</div>
</body>
</html>
HTML_CODE
 
exit(0);
 
?>

Open in new window

0
 
LVL 3

Expert Comment

by:Undisputed
ID: 24143749
My apologies, I left out a ";" on line 36 of the code
<?php
//connect to db
$dbh = new PDO('mysql:host=localhost;dbname=test1', user, password, array( PDO::ATTR_PERSISTENT => true ));
 
if(empty($_GET['TSID']) == FALSE)
{
    $tsid = trim($_GET['TSID']);
    $return_message = 'TSID: ' . $tsid;
 
    //Do some validations here is desired
 
    //Insert into db
    $sql = 'INSERT INTO TS1 (`Field1`) VALUES (:tsid)';
    $rslt = $dbh->prepare($sql);
 
    if($rslt->exec(array('tsid' => $tsid)) or die(print_r($rslt->errorInfo())))
    {
       $return_message .= ' has been inserted into db.';
    }
    else
    {
       $return_message .= ' was not inserted into db.';
    }
 
}
 
//Output html
print <<<HTML_CODE
<html>
<body>
<div id="return_message">
$return_message
</div>
</body>
</html>
HTML_CODE;
 
exit(0);
 
?>

Open in new window

0
 

Author Comment

by:Andy Brown
ID: 24146072
I'm not quite there yet - sorry.

I created a file called insert.php, that I put onto the webserver (in the same folder as index.html).  I put in the following hyperlink:

http://www.callibre.com/insert.php?TSID=123456

And got the following error:

Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in D:\inetpub\wwwroot\vhosts\callibre.com\httpdocs\insert.php:3 Stack trace: #0 D:\inetpub\wwwroot\vhosts\callibre.com\httpdocs\insert.php(3): PDO->__construct('mysql:host=loca...', 'user', 'password', Array) #1 {main} thrown in D:\inetpub\wwwroot\vhosts\callibre.com\httpdocs\insert.php on line 3


Any help would be really appreciated - I'd love to get some form of process in place today.
0
 
LVL 3

Expert Comment

by:Undisputed
ID: 24149770
I haven't used odbc myself but give the code below a shot.
<?php
//connect to db
$connect = odbc_connect("test1", "user", "password");
 
if(empty($_GET['TSID']) == FALSE)
{
    $tsid = trim($_GET['TSID']);
    $return_message = 'TSID: ' . $tsid;
 
    //Do some validations here is desired
 
    //Insert into db
    $sql = 'INSERT INTO TS1 (`Field1`) VALUES (?)';
    $rslt = odbc_prepare($connect, $sql);
 
    if( odbc_execute($rslt, array($tsid)) or die(odbc_errormsg($connect))
    {
       $return_message .= ' has been inserted into db.';
    }
    else
    {
       $return_message .= ' was not inserted into db.';
    }
 
}
 
//Output html
print <<<HTML_CODE
<html>
<body>
<div id="return_message">
$return_message
</div>
</body>
</html>
HTML_CODE;
 
exit(0);
 
?>

Open in new window

0
 

Author Closing Comment

by:Andy Brown
ID: 31570113
Thank you for your help on this one - really sorry for the delay in getting back to you.  I still haven't got round to adding the functionality to my app, but I will use your examples when I do.  Really appreciated and thank you once again.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question