Paste in mysql

I want to pass parameters to a batch file, which will then be inserted into the mysql database.

lunch.bat name surname echo off mysql -uusername -ppassword -e "set @1:=name; set @2:=surname; source insert.sql;" 

In insert.sql

 insert into mytable(namecol,surnamecol) values(@1,@2); 

Can anyone help me write both scenarios?

thanks

+4
source share
2 answers

Single quotes have been added to handle string input ... it is still important if params parameters

 @echo off mysql -uusername -ppassword -e "set @1:='%1'; set @2:='%2'; source insert.sql;" 

Done!

+3
source

Here is a snippet of my code that I worked on, although it is published in the database every time you visit a page. I tried adding a "unique key" using rand () in the database. But the code still captures the "rand () on the page" and the post without the "Submit" from the form. Anyway, this is how I insert the data into Mysql. My database creates a timestamp of time and day () and order by "id".

  // Select from database working $statement = $database->prepare('SELECT * FROM pressroom ORDER BY id DESC LIMIT 0 , 30' ); $statement->execute(); $count = $statement->rowCount(); if( $count > 0 ) { $R = $statement->fetchAll( PDO::FETCH_ASSOC ); for( $x = 0; $x < count($R); $x++ ) { echo "<td><br>"; echo "<center><b>" . $R[ $x ]['name'] . "</th>"; echo ": <left></b>" . $R[ $x ]['comment'] . "</th>"; echo "<center>" . $R[ $x ]['date'] . "<hr></td>"; echo "</tr>"; } } // Make sure that the 'values' are in the same order or as your table $query = "INSERT INTO pressroom(first_name, last_name, Password ) VALUES ('table1','table2','table3')"; //connection prepare try { $database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $results = $database->query($query); //Prints results that are added to table print_r($results); } catch(PDOException $e) { echo 'ERROR: ' . $e->getMessage(); } 
0
source

Source: https://habr.com/ru/post/1341807/


All Articles