开发者

Insert into mysql

I want to 开发者_JAVA技巧pass parameters to a batch file which will then inserted into 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);

Someone can help me in writing both scripts?

Thanks


Added single quotes to handle string input... still wobly if quotes are supplied in the paramaters

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

Done!


Here is a piece of my code that I been working on, although it post to database every time you visit the page. I've tried to add "unique key" with rand() in database. But the code still grabs "rand() on page" and post without "Submit" from form. Anyways this is how I insert data to Mysql. My database creates time and day timestamp() & 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜