Advertisement
sriyanto

main.py

Sep 12th, 2022
2,188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python for S60 2.83 KB | Source Code | 0 0
  1. from flask import Flask, render_template, request, redirect, flash, url_for, session
  2. import mysql.connector
  3. from werkzeug.security import check_password_hash, generate_password_hash
  4. app = Flask(__name__)
  5. app.secret_key = "asdfghjkl12345fdsa_fdsakld8rweodfds"
  6. db = mysql.connector.connect(host="remotemysql.com",user="wAffmdiOi7", passwd="igBJz9WbOS", database="wAffmdiOi7")
  7. cur = db.cursor()
  8.  
  9. @app.route('/')
  10. def home():
  11.     return render_template('index.html')
  12.  
  13. @app.route('/admin')
  14. def customertampildata():
  15.     #cur = mysql.connection.cursor()
  16.     cur = db.cursor()
  17.     cur.execute("SELECT * FROM customer ORDER BY id DESC")
  18.     datatampil = cur.fetchall()
  19.     cur.close()
  20.     return render_template('admin.html', datapemesan=datatampil)
  21.  
  22. #proses insert into
  23. @app.route('/', methods=['POST'])
  24. def customerinsert():
  25.     if request.method == 'POST':
  26.         nama = request.form['nama']
  27.         email = request.form['email']
  28.         phone = request.form['phone']
  29.         tipe = request.form['tipe']
  30.         checkin = request.form['checkin']
  31.         checkout = request.form['checkout']
  32.         jml = request.form['jml']
  33.         ket = request.form['ket']
  34.  
  35.         ##cur = mysql.connection.cursor()
  36.         cur = db.cursor()
  37.         cur.execute("INSERT INTO customer (nama, email, phone, tipe, checkin, checkout, jml, ket) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)", (nama, email, phone, tipe, checkin, checkout, jml, ket))
  38.         db.commit()
  39.         flash("Data Berhasil di kirim")
  40.         return redirect(url_for('home'))
  41.  
  42. #proses insert into
  43. @app.route('/customerupdate', methods=['POST'])
  44. def customerupdate():
  45.     if request.method == 'POST':
  46.         id = request.form['id']
  47.         nama = request.form['nama']
  48.         email = request.form['email']
  49.         phone = request.form['phone']
  50.         tipe = request.form['tipe']
  51.         checkin = request.form['checkin']
  52.         checkout = request.form['checkout']
  53.         jml = request.form['jml']
  54.         status = request.form['status']
  55.  
  56.         #cur = mysql.connection.cursor()
  57.         cur = db.cursor()
  58.         cur.execute("UPDATE customer SET nama=%s, email=%s, phone=%s, tipe=%s, checkin=%s, checkout=%s, jml=%s, status=%s WHERE id=%s", (nama, email, phone, tipe, checkin, checkout, jml, status, id))
  59.         db.commit()
  60.         flash("Data Berhasil di Update")
  61.         return redirect(url_for('customertampildata'))
  62.  
  63. # delete data
  64. @app.route('/customerhapus/<int:id>', methods=["GET"])
  65. def customerhapus(id):
  66.     #cur = mysql.connection.cursor()
  67.     cur = db.cursor()
  68.     cur.execute("DELETE FROM customer WHERE id=%s", (id,))
  69.     db.commit()
  70.     flash("data Berhasil di Hapus")
  71.     return redirect( url_for('customertampildata'))
  72.  
  73. if db.is_connected():
  74.   print("Berhasil terhubung ke database")
  75. if __name__ == '__main__':
  76.     app.run(host='0.0.0.0',debug=True, port=3306)
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement