简体   繁体   中英

How can I use the MySQL record id to name an image?

I have a script that saves the signature from html5 canvas and assigns a random number as the image name then stores it in a folder and posts the name in MySQL.

I already have a record in the MySQL table that will be associated with this image, when that record is edited to include this image I want to save the image with the EXISTING record id.

So basically the script saves the images like this sign_4354533225533.png I want to remove the number generator and replace it with the record id, Meaning, the browser will pass the record id of 42 to signature_pad.php I want to take that id and use it to name my file so it would look like sign_42.png.

Here is my code:

<?php
$id=$_GET['id'];
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
//$session_id = $_SERVER['REMOTE_ADDR'];
// Get the data
$imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
// Remove the headers (data:,) part.
// A real application should use them according to needs such as to check image type
$filteredData=substr($imageData, strpos($imageData, ",")+1);

// Need to decode before saving since the data we received is already base64 encoded
$unencodedData=base64_decode($filteredData);

//echo "unencodedData".$unencodedData;
$sig_name = "sign_" . rand(5,1000) . rand(1, 10) . rand(10000, 150000) . rand(1500, 100000000) . ".png";
//Set the absolute path to your folder (i.e. /usr/home/your-domain/your-folder/
$filepath = "/xampp/htdocs/alpha/development/final/uploads/signatures/" . $sig_name;
$customer_signature=$_POST['customer_signature'];
$fp = fopen("$filepath", 'wb' );
fwrite( $fp, $unencodedData);
fclose( $fp );

//Connect to a mySQL database and store the user's information so you can link to it later
$link = mysql_connect('localhost','root', 'pwd') OR DIE(mysql_error);
mysql_select_db("customer", $link);
mysql_query("UPDATE signature SET customer_signature=$customer_signature WHERE 'id'= '$id'"); OR DIE(mysql_error());
mysql_close($link);
}
?>
 <?php
session_start()
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0   Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Signature Pad</title>

<!-- The Signature Pad -->
< script type ="text/javascript"    src="jquery.min.js"></script>
<script type="text/javascript" src="signature-pad.js"></script>
</head>
<body>
<center>
<fieldset style="width: 435px">
    <br/>
    <br/>
    <div id="signaturePad" style="border: 1px solid #ccc; height: 55px; width: 400px;"></div>
    <br/><br/><br/>
    <button id="clearSig" type="button">Clear Signature</button>&nbsp;
    <button id="saveSig" type="button">Save Signature</button>
    <div id="imgData"></div>
    <br/>
    </fieldset>
</center>
<!--<div id="debug"></div>-->
</body>
</html>

EDIT TO ACCOMODATE YOUR ABOVE CHANGES

So with your $id = $_GET['id']; Just grab the row from the database...

 $id=$_GET['id'];
 $customer_signature=$_POST['customer_signature']; 

 $q = mysql_query("SELECT * FROM signature WHERE id ='$id'");
 $row = mysql_fetch_assoc($q);

 //THEN SAVE IMAGE with the fetched row ID ($row['id'])
 $filepath = "/xampp/htdocs/alpha/development/final/uploads/signatures/".$row['id'];
 $fp = fopen("$filepath", 'wb' );
 fwrite( $fp, $unencodedData);
 fclose( $fp );


 //THEN WHEN YOU SAVE IT BACK ITS AN UPDATE
 mysql_query("UPDATE signature SET 'customer_signature'='".$_POST['customer_signature']."', 'sig_name'=$sig_name" WHERE id = ".$id);

BUT... WARNING

You really should not be using the deprecated mysql_* functions. Instead google PDO, learn it, and use that instead :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM