Jump to content

Input text string to database then print to image


chrisb302

Recommended Posts

Hello, I am attempting to create a script where a user puts a text string into a form and clicking on submit. Then it adds that text string to a mysql database and gives the user a link back to it. Something like mysite.com/key.php?id=4 and when going to that link it will display the text string from the database but printed in an image. I wrote something up but I cannot get it to work. I may be a little in over my head. Any help is appreciated.

 

The way I coded this all to work is the user inputs the information into index.php which givekey.php inserts that into the database. I have not yet figure out how to do this in one single step. After that I wanted it to navigate directly to key.php which would display the key and the link to that page with the ?key=XX attribute.
 
getkey.php

<?php
  require_once 'dbinfo.php';
  // database connection
 
  $id = $_GET['id'];
  // do some validation here to ensure id is safe
 
  $link = mysql_connect($servername, $username, $password);
  if (!$link) {
            die('Could not connect: ' . mysql_error());
            }
  @mysql_select_db($database) or die( "Unable to select database");
  $sql = "SELECT ukey FROM keycode WHERE id=$id";
  $result = mysql_query("$sql");
  $row = mysql_fetch_assoc($result);
  mysql_close($link);
 
  header("Content-type: image/png");
  echo $row['ukey'];
?>

 
key.php

<html>
 
<head>
  <title>Your Key Is Ready</title>
</head>
 
<body>
<img src="getkey.php?id=1" width="175" height="200" />
</body>
 
</html>

index.php

<html>

<head>
  <title></title>
</head>

<body>

<section id="mid_section">
                <div id="boxes">
                    <h1>
                        Testing input key
                    </h1>
                    <br/>
                    <form id="myform" action="givekey.php" method="post">
                        Key:<br />
                        <input type="text" value="ukey">
                        Source:<br />
                        <input type="radio" value="hb">HB<br />
                        <input type="radio" value="ig">IG<br />
                        <input type="radio" value="other">Other<br />
                        <button id="sub">Submit</button>
                    </form>

</body>

</html>

givekey.php

<?php
    include_once('dbinfo.php');

    $conn = mysql_connect($servername, $username, $password);
    $db= mysql_select_db($database);

    $ukey =$_POST['ukey'];
    $hb =$_POST['hb'];
    $ig =$_POST['ig'];
    $other =$_POST['other'];

    if(mysql_query("INSERT INTO `keycode`(`ukey`) VALUES ([$ukey]); INSERT INTO `source`(`hb`,`ig`,`other`) VALUES ([$hb],[$ig],[$other]);"))
?>
Edited by chrisb302
Link to comment
Share on other sites

Fixed typos in this file.

 

givekey.php

<?php
    include_once('dbinfo.php');

    $conn = mysql_connect($servername, $username, $password);
    $db= mysql_select_db($database);

    $ukey =$_POST['ukey'];
    $hb =$_POST['hb'];
    $ig =$_POST['ig'];
    $other =$_POST['other'];

    $key = "INSERT INTO keycode (ukey) VALUES ('$ukey')";
    $source ="INSERT INTO source (hb,ig,other) VALUES ('$hb','$ig','$other')";

    if(mysql_query($key, $source));

?>
Link to comment
Share on other sites

Not sure what isnt working for you, I have some ideas from how things are laid out but....

Some things right off the bat, look into mysqli over mysql to stay current.

 

 

 

the user inputs the information into index.php which givekey.php inserts that into the database. I have not yet figure out how to do this in one single step

you can post the form data back to itself in the (action="index.php")

At the top of index.php

 

add something like this 

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//add your form processing code here
}
?>

so when the submit button is pressed the above will run.

 

next - give some names to your inputs so your form processing code knows what to do with what info.

 

for example

<input type="text" name="ukey" value="ukey">

then your form process would do something like this

// Trim all the incoming data:
    $trimmed = array_map('trim', $_POST);
    // Assume invalid values:
    $ukey = $hb = $ig = $other = FALSE;

    // Validate the input Key string:
    if (!empty($trimmed['ukey'])) {
        $ukey = mysqli_real_escape_string ($dbc, $trimmed['ukey']);
    } else {
        $ukey = FALSE;
        echo '<p class="error">You forgot to enter a key!</p>';
    }.....and so on with the rest of the form

to continue with the one step process you could use 

http://php.net/manual/en/mysqli.insert-id.php

 

so once the data is added to the db successfully it will pass the last ID to your image create function.

 

As for the image with the text on it, not sure how far you got with that but you can look into the gd image functions - like ...

 

this is a start if any of it makes sense.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.