2

I've drawn a blank. (Ran out of Caffeine). I'm trying to create a Trigger in Phpmyadmin that will add two characters "MR" into each row. I don't want the characters added to the primary id but in a column labeled mer_sku. Also other data will be added into this mer_sku col from a html form so the data needs to be concat.

Just to clarify, Im trying to make the trigger within the PHPmyadmin GUI

Thanks!

FORGOT TO ADD WHAT I HAVE TRIED

enter image description here

ERROR IS ON LINE 1

enter image description here

8
  • yes. I forgot to add the screenshot. :( Commented Nov 1, 2016 at 0:05
  • SET NEW.mer_sku = CONCAT(NEW.mer_sku, 'MR') Commented Nov 1, 2016 at 0:10
  • thanks for this. However, I get a syntax error code. #1064? Not sure where the syntax error is. Commented Nov 1, 2016 at 0:19
  • You have ; at the end of the line? Commented Nov 1, 2016 at 0:31
  • The error message should tell you where the syntax error is. Commented Nov 1, 2016 at 0:31

2 Answers 2

1

So let post my code first and then i'll explain my motivation and another issue that has presented itself that i'm trying to resolve.

?php 
if (isset($_POST['submit'])) {
    $mfr   = $_POST['mfr'];
    $type  = $_POST['type'];
    $desc  = $_POST['description'];
    $price = $_POST['price'];
    $qty   = $_POST['qty'];

    $last_id = 0;
    $sql = "SELECT m_id FROM merchandise";
    $result = mysqli_query($connection, $sql);
    while($row = mysqli_fetch_assoc($result)) {
    $last_id = $row["m_id"];
    }
    $next_id = $last_id+1;
    $conc = number_format($next_id/100,2,'-','');

    $query = "INSERT INTO merchandise (mfr,type,description,mer_sku,price,qty) ";
    $query .="VALUES ('$mfr','$type','$desc','MR{$mfr}{$conc}','$price','$qty')";
    $add_sku_query = mysqli_query($connection, $query);
    confirmQuery($add_sku_query);
    $alert = <<<DELIMETER

<div class='alert alert-warning alert-dismissible fade in' role='alert'>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
</button>
<strong>Inventory Updated!</strong> <a href='inventory.php?view_all_inventory'>View All Inventory</a>
</div>
DELIMETER;

    echo $alert;
    }
?>

So first off let me point out that I did try to set this up using PDO but i'm not yet fully comprehending it so i'm sticking with mysqli. Which i know that how I have the code setup it leaves it vulnerable to SQL Injection.

So that aside, My motivation was to try and add items into a table sequentially. However, my code currently auto increments without regards to the $mfr variable which is ESSENTIAL. Each manufacturer has a unique value and so the auto increment would NEED to auto increment PER manufacturer instead of auto increment while disregarding the the manufacturer variable that is concatenated.

**CURRENT AUTO INCREMENT, [NOT WHAT I WHAT]**
MR500-01
MR600-02
MR700-03
MR500-04
MR600-05
MR700-06

**IDEAL AUTO INCREMENT, [WHAT I WANT]**
MR500-01
MR600-01
MR700-01
MR500-02
MR600-02
MR700-02

I hope I explained my thought. Anyways, this is my current status and I'm working towards creating the resolution that I NEEED.

Hope this was somewhat helpful to anyone or if someone has some helpful input that would be great and much appreciated. Thanks.

Sign up to request clarification or add additional context in comments.

1 Comment

I found this question that is similar. However, this is using a MyISAM DB engine and to autoincrement the primary key which I don't want to do. So based on the linked question. The result i'm looking for is close to this. stackoverflow.com/questions/3605085/…
0

Instead of GUI, try running it in the query section. I think the error is because mysql treats ; as the end of your trigger declaration.

So redefine delimiter before writing code for the trigger

Try the below code

delimiter |

CREATE TRIGGER `merch_label` 
BEFORE INSERT ON `merchandise` 
FOR EACH ROW  
BEGIN  
    SET NEW.mer_sku = CONCAT(NEW.mer_sku, 'MR'); 
END;
|

delimiter ;

3 Comments

this does not work still. I'm continuing to get an error. Unrecognized statement type. (near "BEFORE" at position 29)
Or I will get this error. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END' at line 8
The Trigger function didn't work however. I made some changes and I was able to get my code to work. During this process i discovered another issue. I'll update this post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.